StringBuilder class is used to create mutable (modifiable) String. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized.
A list of differences between StringBuffer and StringBuilder is given below:
public class StringBuilderExample {
public static void main(String[] args) {
// Creating a StringBuilder
StringBuilder stringBuilder = new StringBuilder("Hello");
// Appending characters
stringBuilder.append(" World");
// Inserting characters at a specific position
stringBuilder.insert(5, ", ");
// Deleting characters
stringBuilder.delete(11, 16);
// Updating characters
stringBuilder.replace(6, 11, "Java");
// Converting to String
String result = stringBuilder.toString();
System.out.println(result); // Output: Hello Java, World
}
}
Комментариев нет:
Отправить комментарий