Explain the methods commonly used in the StringBuffer class in Java

  • 2020-04-01 04:35:13
  • OfStack

Since String is an invariant class, modifying a String with String will create a new String object, and if it is modified frequently, many String objects will be generated, which is expensive. Therefore, Java provides a StringBuffer class, which is much more efficient than String in modifying strings.
There are three classes in Java that handle character manipulation.

  1.Character is a single Character operation.
  2.String operations on a String of characters, immutable class.
  3.StringBuffer also operates on a string of characters, but with a mutable class.

public class UsingStringBuffer { 
   
  public static void testFindStr() { 
    StringBuffer sb = new StringBuffer(); 
    sb.append("This is a StringBuffer"); 
    //Returns the first position of the substring in the string, or a negative number if it does not exist
    System.out.println("sb.indexOf("is")=" + sb.indexOf("is")); 
    //Set parameters to the indexOf method, specifying the starting position of the match
    System.out.println("sb.indexOf("is")=" + sb.indexOf("is", 3)); 
    //Returns the last position of the substring in the string, or a negative number if it does not exist
    System.out.println("sb.lastIndexOf("is") = " + sb.lastIndexOf("is")); 
    //Set parameters to the lastIndexOf method, specifying the end of the match
    System.out.println("sb.lastIndexOf("is", 1) = " 
        + sb.lastIndexOf("is", 1)); 
  } 
 
   
  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 a string, and you can specify where the interception starts and ends
    System.out.print("sb.substring(4,9)=" + sb.substring(4, 9)); 
  } 
 
   
  public static void testCharAtStr() { 
    StringBuffer sb = new StringBuffer("This is a StringBuffer"); 
    System.out.println(sb.charAt(sb.length() - 1)); 
  } 
 
   
  public static void testAppend() { 
    StringBuffer sb = new StringBuffer("This is a StringBuffer!"); 
    sb.append(1.23f); 
    System.out.println(sb.toString()); 
  } 
 
   
  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()); 
  } 
 
   
  public static void testInsert() { 
    StringBuffer sb = new StringBuffer("This is a StringBuffer!"); 
    //The ability to insert characters, character arrays, strings, and various numeric and Boolean values at specified locations
    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()); 
  } 
 
   
  public static void testReplace() { 
    StringBuffer sb = new StringBuffer("This is a StringBuffer!"); 
    //Replaces a segment of a string with another string
    sb.replace(10, sb.length(), "Integer"); 
    System.out.println("testReplace: " + sb.toString()); 
  } 
 
   
  public static void reverseStr() { 
    StringBuffer sb = new StringBuffer("This is a StringBuffer!"); 
    System.out.println(sb.reverse()); //The reverse method reverts strings
  } 
} 

Summary:
StringBuffer is not an invariant class and does not create new objects when modifying the String content, so it is better suited to modifying strings than the String class.
The StringBuffer class does not provide the same toCharArray method as String;
Unlike the replace method of the String class, the replace method of the StringBuffer class has three arguments: the first parameter specifies the starting position of the replaced substring, the second parameter specifies the ending position of the replaced substring, and the third parameter specifies the new substring.

The above is the entire content of this article, I hope to help you with your study.


Related articles: