String is an invariant class. Using String to modify a string will create a new String object. If it is modified frequently, many String objects will be generated, which is very expensive. Therefore, Java provides a StringBuffer class, which is much more efficient than String in modifying strings.
There are 3 classes in Java that are responsible for character operations.
public class UsingStringBuffer { /** * Find matching string*/ public static void testFindStr() { StringBuffer sb = new StringBuffer(); sb.append("This is a StringBuffer"); // Returns the position where the substring appears first in the string. If it does not exist, return the negative number System.out.println("sb.indexOf(/"is/")=" + sb.indexOf("is")); // Set parameters for the indexOf method and specify the matching starting position System.out.println("sb.indexOf(/"is/")=" + sb.indexOf("is", 3)); // Returns the last position of the substring in the string. If it does not exist, return the negative number System.out.println("sb.lastIndexOf(/"is/") = " + sb.lastIndexOf("is")); // Set parameters for the lastIndexOf method and specify the matching end position System.out.println("sb.lastIndexOf(/"is/", 1) = " + sb.lastIndexOf("is", 1)); } /** * Intercept the string*/ public static void testSubStr() { StringBuffer sb = new StringBuffer(); sb.append("This is a StringBuffer"); // The default termination position is the end of the string System.out.print("sb.substring(4)=" + sb.substring(4)); // The substring method intercepts the string, which can specify the starting position and the termination position of the intercept System.out.print("sb.substring(4,9)=" + sb.substring(4, 9)); } /** * Get the characters at a certain position in the string */ public static void testCharAtStr() { StringBuffer sb = new StringBuffer("This is a StringBuffer"); System.out.println(sb.charAt(sb.length() - 1)); } /** * Add various types of data to the tail of the string*/ public static void testAppend() { StringBuffer sb = new StringBuffer("This is a StringBuffer!"); sb.append(1.23f); System.out.println(sb.toString()); } /** * Delete data in string*/ public static void testDelete() { StringBuffer sb = new StringBuffer("This is a StringBuffer!"); sb.delete(0, 5); sb.deleteCharAt(sb.length() - 1); System.out.println(sb.toString()); } /** * Insert various types of data into strings*/ public static void testInsert() { StringBuffer sb = new StringBuffer("This is a StringBuffer!"); // Can insert characters, character arrays, strings, and various numbers and boolean values at specified positions sb.insert(2, 'W'); sb.insert(3, new char[] { 'A', 'B', 'C' }); sb.insert(8, "abc"); sb.insert(2, 3); sb.insert(3, 2.3f); sb.insert(6, 3.75d); sb.insert(5, 9843L); sb.insert(2, true); System.out.println("testInsert: " + sb.toString()); } /** * Replace some characters in a string*/ public static void testReplace() { StringBuffer sb = new StringBuffer("This is a StringBuffer!"); // Replace a certain character in a string with another string sb.replace(10, sb.length(), "Integer"); System.out.println("testReplace: " + sb.toString()); } /** * Reverse string*/ public static void reverseStr() { StringBuffer sb = new StringBuffer("This is a StringBuffer!"); System.out.println(sb.reverse()); // reverse method reverses string} } summary:
StringBuffer is not an invariant class. When modifying the content of a string, no new objects are created. Therefore, it is more suitable for modifying strings than String classes;
The StringBuffer class does not provide the same toCharArray method as String;
The replace method of the StringBuffer class is different from the replace method of the String class. Its replace method has three parameters. The first parameter specifies the start position of the replaced substring, the second parameter specifies the end position of the replaced substring, and the third parameter specifies the new substring.
The above is all about this article, I hope it will be helpful to everyone's learning.