Java Append( ) method : String vs. StringBuilder

2014-04-29T09:22:52

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?

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
*/

Copyright License:
Author:「Michael」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/23353926/java-append-method-string-vs-stringbuilder

About “Java Append( ) method : String vs. StringBuilder” questions

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
With this code: public static void main(String[] args) { String s = "Java"; StringBuilder buffer = new StringBuilder(s); change(buffer); System.out.println("What's strBuf.ch...
reference to append is ambiguous rez.append(ROOT_FORM.containsKey(text.charAt(i)) ? ROOT_FORM.get(text.charAt(i)) : text.charAt(i)); ^ both method append(Object) in StringBuilder and method append(...
According to Netbeans hint named Use chain of .append methods instead of string concatenation Looks for string concatenation in the parameter of an invocation of the append method of StringBuild...
I get the following errors: nameCombine.java:18: error: cannot find symbol sb.append(s);" ^ symbol: method append(String) location: variable sb of type StringBuilder nameCombine.java:19: error:
I have a legacy Java file which uses String concatenation to build huge String objects.Its a serious performance issue.Is there a method as such which does the following String test="I am a very bad
I have a question regarding the append method in the StringBuilder class. I was asked how can we override the append() method in the StringBuilder class while stringBuilder class is final. Is the s...
What is the best practice when using string builders in .NET regarding multiple calls of the .Append method vs. a single call to .Append while concatenating its parameters? sb.Append(name).Append(...
I know that using most of String's methods will return a new string object which is added to the string pool and that the string literals passed as arguments to these methods also are added to the ...
I tried to append a long String (length of 3000) with java StringBuilder and found out that the appended result is not what I expected. A.append(B) should be AB A.append(LongString) becomes

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.