StringBuilder and StringBuffer Methods

StringBuilder and StringBuffer classes are used to manipulate strings in Java. Both of these classes are similar in many ways, but they differ in the way they handle thread safety. StringBuffer is thread-safe, which means it can be used in multithreaded environments without any issues. On the other hand, StringBuilder is not thread-safe, which means it can be used in single-threaded environments only.

In this article, we will discuss the common methods present in String, StringBuilder, and StringBuffer classes, along with the methods that are present only in StringBuilder and StringBuffer classes. We will also provide examples of how to use these methods in Java.

Common Methods

The following methods are common to the String, StringBuilder, and StringBuffer classes:

  • length(): This method returns the number of characters the StringBuilder object contains.
  • charAt(index): This method returns the character at the specified index of the String contained by the StringBuilder object.
  • indexOf(str): This method returns the index within the String for the first occurrence of the passed substring as a parameter.
  • indexOf(str, fromIndex): This method returns the index within the String for the first occurrence of the passed substring as a parameter, starting at the specified index.
  • lastIndexOf(str): This method returns the index within the String for the last occurrence of the passed substring as a parameter.
  • lastIndexOf(str, fromIndex): This method returns the index of a substring within the original String, starting from 0 index to the index fromIndex.
  • compareTo(sb): This method compares two strings lexicographically.
  • substring(beginIndex): This method returns a substring starting from beginIndex and extending to the end of this sequence.
  • substring(beginIndex, lastIndex): This method returns a substring starting from beginIndex and extending to the index lastIndex-1 of this sequence.
  • chars(): This method returns a stream of int zero-extending the char values from this sequence.

Examples:

StringBuilder strBuilder = new StringBuilder("Hello World"); System.out.println(strBuilder.length()); // Output: 11

StringBuilder strBuilder = new StringBuilder("Hello World"); System.out.println(strBuilder.charAt(6)); // Output: W

StringBuilder strBuilder = new StringBuilder("Hello World"); System.out.println(strBuilder.indexOf("o")); // Output: 4

StringBuilder strBuilder = new StringBuilder("Hello World"); System.out.println(strBuilder.lastIndexOf("o")); // Output: 7

StringBuilder strBuilder = new StringBuilder("Hello World"); System.out.println(strBuilder.substring(6)); // Output: World

StringBuilder strBuilder = new StringBuilder("Hello World"); System.out.println(strBuilder.substring(0, 5)); // Output: Hello

StringBuilder strBuilder = new StringBuilder("Hello World"); strBuilder.chars().forEach(System.out::println); // Output: The ASCII values of all characters in the StringBuilder object

Methods in StringBuilder and StringBuffer Classes Only

The following methods are present only in the StringBuilder and StringBuffer classes:

  • append(x): This method appends the string representation of some argument to the sequence.
  • insert(offset, x): This method inserts the string representation of a given data type at the given offset position in a StringBuffer.
  • setCharAt(index, c): This method sets the character at the position index passed as c.
  • reverse(): This method reverses the characters in the StringBuilder.
  • deleteCharAt(index): This method removes the character at the given index from the String contained by the StringBuilder.
  • delete(start, end): This method removes the characters starting from index start to index end-1 from the String contained by the StringBuilder.
  • capacity(): This method returns the current capacity of the StringBuilder object.
  • replace(start, end, str): This method replaces the characters in a substring of this sequence with characters in the specified String.

Examples:

StringBuilder strBuilder = new StringBuilder("Hello"); strBuilder.append(" world!");

System.out.println(strBuilder);

In this example, we create a StringBuilder object and initialize it with the String "Hello". Then we use the append() method to add " world!" to the end of the StringBuilder object. Finally, we print the contents of the StringBuilder object to the console.

Another example using the insert() method:

StringBuilder strBuilder = new StringBuilder("Hello"); strBuilder.insert(3, " world"); System.out.println(strBuilder);

In this example, we create a StringBuilder object and initialize it with the String "Hello". Then we use the insert() method to insert " world" at index 3 of the StringBuilder object. Finally, we print the contents of the StringBuilder object to the console.

One more example using the delete() method:

StringBuilder strBuilder = new StringBuilder("Hello world"); strBuilder.delete(5, 11); System.out.println(strBuilder);

In this example, we create a StringBuilder object and initialize it with the String "Hello world". Then we use the delete() method to remove the characters from index 5 to index 11 (exclusive) from the StringBuilder object. Finally, we print the contents of the StringBuilder object to the console.

Overall, StringBuilder and StringBuffer are very useful classes in Java for manipulating strings efficiently. By using their various methods, we can perform operations such as appending, inserting, deleting, and replacing characters in a StringBuilder or StringBuffer object, which can be more efficient than creating and manipulating multiple String objects.

Previous Post Next Post