Java Append() 方法:String 与 StringBuilder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23353926/
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
Java Append( ) method : String vs. StringBuilder
提问by Michael
When appending the subset of a String to a String object, the last position in the number of characters specified by the append statement is NOT included as part of the return object. However, when appending the subset of an char Array to a char Array object, the last position in the number of characters specified by the append statement is included as part of the return object. Why is this?
将 String 的子集附加到 String 对象时,由 append 语句指定的字符数中的最后一个位置不包括在返回对象中。但是,当将 char Array 的子集附加到 char Array 对象时,由 append 语句指定的字符数中的最后一个位置作为返回对象的一部分包含在内。为什么是这样?
Example -
例子 -
class String {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder();
String name = "Java7";
sb.append(name, 1,3);
System.out.println(sb);
}
}
/* result is:
av
*/
class char {
public static void main(String args[]) {
StringBuilder sb = new StringBuilder();
char[] name = {'J', 'A', 'V', 'A', '7'};
sb.append(name, 1,3);
System.out.println(sb);
}
}
/* result is:
AVA
*/
采纳答案by sleepyfung
when char[] http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#append(char[], int, int)
当 char[] http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#append(char[], int, int)
Appends the string representation of a subarray of the char array argument to this sequence. Characters of the char array str, starting at index offset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value of len.
将 char 数组参数的子数组的字符串表示形式附加到此序列。char 数组 str 的字符,从索引 offset 开始,按顺序附加到此序列的内容。这个序列的长度增加了 len 的值。
The overall effect is exactly as if the arguments were converted to a string by the method String.valueOf(char[],int,int), and the characters of that string were then appended to this character sequence.
整体效果就像通过 String.valueOf(char[],int,int) 方法将参数转换为字符串,然后将该字符串的字符附加到此字符序列。
Parameters: str - the characters to be appended. offset - the index of the first char to append. len - the number of chars to append.
参数: str - 要附加的字符。offset - 要追加的第一个字符的索引。len - 要附加的字符数。
when String http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#append(java.lang.CharSequence, int, int)
当字符串 http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#append(java.lang.CharSequence, int, int)
Appends a subsequence of the specified CharSequence to this sequence. Characters of the argument s, starting at index start, are appended, in order, to the contents of this sequence up to the (exclusive) index end. The length of this sequence is increased by the value of end - start.
将指定的 CharSequence 的子序列附加到此序列。参数 s 的字符从索引 start 开始,按顺序附加到此序列的内容,直到(独占)索引结束。这个序列的长度增加了 end - start 的值。
Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in this character sequence becomes equal to the character at index k in this sequence, if k is less than n; otherwise, it is equal to the character at index k+start-n in the argument s.
令 n 为执行 append 方法之前的此字符序列的长度。如果 k 小于 n,则该字符序列中索引 k 处的字符等于该序列中索引 k 处的字符;否则,它等于参数 s 中索引 k+start-n 处的字符。
If s is null, then this method appends characters as if the s parameter was a sequence containing the four characters "null".
如果 s 为空,则此方法附加字符,就好像 s 参数是包含四个字符“空”的序列一样。
Parameters: s - the sequence to append. start - the starting index of the subsequence to be appended. end - the end index of the subsequence to be appended. Returns: a reference to this object.
参数: s - 要追加的序列。start - 要附加的子序列的起始索引。end - 要附加的子序列的结束索引。返回: 对该对象的引用。
So when you append char[] using sb.append(name, 1,3); means start from index 1, and need a 3 characters.
因此,当您使用 sb.append(name, 1,3); 附加 char[] 时;表示从索引 1 开始,需要 3 个字符。
and String sb.append(name, 1,3); means start from index 1 and stop at index 3, but not including 3, which is a (3-1)=2 long string
和字符串 sb.append(name, 1,3); 表示从索引 1 开始到索引 3 停止,但不包括 3,这是一个 (3-1)=2 长字符串
回答by Sotirios Delimanolis
The methods are different. The 3rd argument in the one that accepts a char[]
is the length, ie the number of characters to append.
方法不一样。接受 achar[]
的第三个参数是长度,即要附加的字符数。
Characters of the char array
str
, starting atindex
offset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value oflen
.
str
从index
offset开始的 char 数组的字符按顺序附加到此序列的内容中。这个序列的长度增加了 的值len
。
The one that accepts a CharSequence
(a String
in your example) says
接受 aCharSequence
(String
在您的示例中为 a)的那个说
Characters of the argument
s
, starting at indexstart
, are appended, in order, to the contents of this sequence up to the (exclusive) indexend
.
参数的字符
s
,从 index 开始start
,按顺序附加到这个序列的内容,直到(独占) indexend
。