StringBuilder class
StringBuilder
class is a class used in Java for handling mutable strings, which provides methods for modification inside the string, in contrast,String
The class is immutable, and a new string object is created every time a string is modified. Therefore, if you need to modify strings frequently, useStringBuilder
Classes will be more efficient.
The following isStringBuilder
Some common methods of classes:
-
append(String str)
:Append the specified string to the currentStringBuilder
the end of the object. -
insert(int offset, String str)
: Insert the specified string at the specified position. -
delete(int start, int end)
: Delete the substring from the starting position to the ending position (excluding the ending position). -
deleteCharAt(int index)
: Delete the characters at the specified position. -
replace(int start, int end, String str)
: Replace the substring from the starting position to the ending position with the specified string. -
reverse()
:Reverse the currentStringBuilder
A sequence of characters in the object. -
length()
: Returns the length of the current character sequence. -
charAt(int index)
: Returns the character at the specified index position. -
substring(int start)
andsubstring(int start, int end)
: Used to obtain a substring, the start position and end position can be passed. -
toString()
:WillStringBuilder
object converted to a plain immutableString
object.
becauseStringBuilder
It 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, useStringBuilder
Classes 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 pairStringBuilder
Detailed introduction and common uses of the methods listed in:
-
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
-
-
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
-
-
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
-
-
deleteCharAt(int index)
:-
Common scenarios:Delete the character at the specified position.
-
Example usage:
StringBuilder sb = new StringBuilder("Hello"); (1); (()); // Output: Helo
-
-
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
-
-
reverse()
:-
Common scenarios:Reverse the string contents.
-
Example usage:
StringBuilder sb = new StringBuilder("Hello"); (); (()); // Output: olleH
-
-
length()
:-
Common scenarios:Returns the length of the current character sequence.
-
Example usage:
StringBuilder sb = new StringBuilder("Hello"); (()); // Output: 5
-
-
charAt(int index)
:-
Common scenarios:Returns the character at the specified index.
-
Example usage:
StringBuilder sb = new StringBuilder("Hello"); ((2)); // Output: l
-
-
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
-
-
toString()
:-
Common scenarios:Will
StringBuilder
object converted to a plain immutableString
object. -
Example usage:
StringBuilder sb = new StringBuilder("Hello World"); String str = (); (str); // Output: Hello World
-
Using these methods, you canStringBuilder
Various modification operations can be performed on the strings in the string to process string concatenation, deletion, replacement and other operations more efficiently.