java StringBuilder
时间: 2024-05-19 16:04:26 浏览: 56
Java StringBuilder
The StringBuilder class in Java is a mutable sequence of characters. It allows the creation and modification of strings without creating a new object every time. StringBuilder is faster than String for concatenation, as it does not need to create a new object for each concatenation operation.
Some of the methods in the StringBuilder class are:
1. append() - appends the specified string to the end of the current string
2. insert() - inserts the specified string at the specified position in the current string
3. replace() - replaces the characters in the string between the specified start and end indices with the specified string
4. delete() - deletes the characters in the string between the specified start and end indices
5. reverse() - reverses the order of the characters in the string
Example:
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // prints "Hello World"
sb.insert(5, " there");
System.out.println(sb); // prints "Hello there World"
sb.replace(11, 16, ",");
System.out.println(sb); // prints "Hello there, World"
sb.delete(11, 12);
System.out.println(sb); // prints "Hello there World"
sb.reverse();
System.out.println(sb); // prints "dlroW ereht olleH"
阅读全文