Location>code7788 >text

Several commonly used methods of StringBuilder class in Java

Popularity:716 ℃/2025-01-17 19:35:16

StringBuilder class

StringBuilderclass is a class used in Java for handling mutable strings, which provides methods for modification inside the string, in contrast,StringThe class is immutable, and a new string object is created every time a string is modified. Therefore, if you need to modify strings frequently, useStringBuilderClasses will be more efficient.

The following isStringBuilderSome common methods of classes:

  1. append(String str):Append the specified string to the currentStringBuilderthe end of the object.

  2. insert(int offset, String str): Insert the specified string at the specified position.

  3. delete(int start, int end): Delete the substring from the starting position to the ending position (excluding the ending position).

  4. deleteCharAt(int index): Delete the characters at the specified position.

  5. replace(int start, int end, String str): Replace the substring from the starting position to the ending position with the specified string.

  6. reverse():Reverse the currentStringBuilderA sequence of characters in the object.

  7. length(): Returns the length of the current character sequence.

  8. charAt(int index): Returns the character at the specified index position.

  9. substring(int start)andsubstring(int start, int end): Used to obtain a substring, the start position and end position can be passed.

  10. toString():WillStringBuilderobject converted to a plain immutableStringobject.

becauseStringBuilderIt is variable and can be modified multiple times without creating a new object each time. Therefore, in scenarios where string content needs to be changed frequently, useStringBuilderClasses can improve efficiency.

The following are specific usage examples of these methods of the StringBuilder class:

Examples of usage of various methods

The following is the pairStringBuilderDetailed introduction and common uses of the methods listed in:

  1. append(String str)

    • Common scenarios:Used to append the specified string to the end of the current string.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       ("World");
       (()); // Output: Hello World
  2. insert(int offset, String str)

    • Common scenarios:Inserts a string at the specified position.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       (5, "World");
       (()); // Output: Hello World
  3. delete(int start, int end)

    • Common scenarios:Delete a substring from the starting position to the ending position (not including the ending position).

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello World");
       (5, 11);
       (()); // Output: Hello
  4. deleteCharAt(int index)

    • Common scenarios:Delete the character at the specified position.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       (1);
       (()); // Output: Helo
  5. replace(int start, int end, String str)

    • Common scenarios:Replaces a substring from the start position to the end position with the specified string.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       (1, 4, "i");
       (()); // Output: Hilo
  6. reverse()

    • Common scenarios:Reverse the string contents.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       ();
       (()); // Output: olleH
  7. length()

    • Common scenarios:Returns the length of the current character sequence.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       (()); // Output: 5
  8. charAt(int index)

    • Common scenarios:Returns the character at the specified index.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello");
       ((2)); // Output: l
  9. substring(int start)andsubstring(int start, int end)

    • Common scenarios:Gets a substring, passing the start and end positions as needed.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello World");
       ((3)); // Output: lo World
       ((3, 7)); // Output: lo W
  10. toString()

    • Common scenarios:WillStringBuilderobject converted to a plain immutableStringobject.

    • Example usage:

      StringBuilder sb = new StringBuilder("Hello World");
       String str = ();
       (str); // Output: Hello World

Using these methods, you canStringBuilderVarious modification operations can be performed on the strings in the string to process string concatenation, deletion, replacement and other operations more efficiently.