java “偏移”在编程上下文中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3971598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "offset" mean in the context of programming?
提问by skystar7
What does "offset" mean in the context of programming?
“偏移”在编程上下文中是什么意思?
Does it mean in the beginning or by a distance?
是一开始的意思还是远处的意思?
What does the String.offsetByCodePoints(int index, int codePointOffset)
method do? What does "unpaired surrogates" in the method documentation mean?
什么是String.offsetByCodePoints(int index, int codePointOffset)
方法吗?方法文档中的“未配对代理”是什么意思?
回答by Stephen C
What does "offset" mean in the context of programming? Does it mean in the beginning or by a distance?
“偏移”在编程上下文中是什么意思?是一开始的意思还是远处的意思?
It means some form of distance from some given position. (What this means will be highly context dependent. In this context, the offset is "measured" in terms of Unicode codepoint positions.)
这意味着距某个给定位置的某种形式的距离。(这意味着高度依赖于上下文。在这种情况下,偏移量是根据 Unicode 代码点位置“测量”的。)
What does the
String.offsetByCodePoints(int index, int codePointOffset)
method do?
什么是
String.offsetByCodePoints(int index, int codePointOffset)
方法吗?
It calculates position of a specific char
within the String
. That char
is the first char of the Unicode codepoint that is codePointOffset
codepoints after the position given by index
.
它计算特定对象char
在String
. 这char
是 Unicode 代码点的第一个字符,它是codePointOffset
由 给出的位置之后的代码点index
。
Both index
and the result are normal string index values; i.e. they are char
positions.
两者index
,结果是正常的字符串的索引值; 即他们是char
职位。
The point ... is that when you are treating a String
as sequence of Unicode codepoints, your code needs to take account of the fact that a codepoint may consist of either 1 or 2 char
values.
关键是……当您将 aString
视为 Unicode 代码点序列时,您的代码需要考虑到代码点可能由 1 个或 2 个char
值组成的事实。
What does "unpaired surrogates" in the method documentation mean?
方法文档中的“未配对代理”是什么意思?
Java strings represent characters that are Unicode code-points > 65535 as UTF-16 surrogate characters. In a well-formed UTF-16 string, the surrogates come in pairs, representing respectively the high and low order bits of the Unicode code-point.
Java 字符串将 Unicode 代码点 > 65535 的字符表示为 UTF-16 代理字符。在格式良好的 UTF-16 字符串中,代理项成对出现,分别代表 Unicode 代码点的高位和低位。
The sentence is saying is that if a String
contains surrogates that are not properly paired, it will treat them as separate codepoints ... for the purpose of counting code points.
这句话的意思是,如果 aString
包含未正确配对的代理,它将把它们视为单独的代码点......以便计算代码点。
See also: What is a "surrogate pair" in Java?
另请参阅:Java 中的“代理对”是什么?
回答by Ryan Berger
According to the JavaDoc,
根据 JavaDoc,
String.offsetByCodePoints(int index, int codePointOffset)
Returns the index within this object that is offset from {@code index} by {@code codePointOffset} code points.
返回此对象中从 {@code index} 偏移 {@code codePointOffset} 代码点的索引。
Here is an example of usage...
这是一个使用示例...
int num = 0;
num = "Test_String".offsetByCodePoints(0, 2); //num is 2
num = "Test_String".offsetByCodePoints(3, 2); //num is 5
num = "Test_String".offsetByCodePoints(9, 5); //Throws an exception since offset goes out-of-bounds
回答by Saken
An example from wikipedia, let's say you have a string "abcdef" the 'd' character will have an offset of three starting from character 'a'.
维基百科的一个例子,假设你有一个字符串“abcdef”,“d”字符将从字符“a”开始偏移三个。