A string is a invariant class, and modifying a string with string creates a new string object that, if frequently modified, produces a lot of string objects. Expensive. So Java provides a stringbuffer class that is much more efficient at modifying strings than string.
There are 3 classes in Java that are responsible for the operation of characters.
- 1.Character is a single character operation,
- 2.String operation on a string of characters, immutable class.
- 3.StringBuffer is also an operation on a string of characters, but a mutable class.
public class Usingstringbuffer {/** * Lookup matching String */public static void Testfindstr () {StringBuffer SB =
New StringBuffer ();
Sb.append ("This is a StringBuffer");
Returns the position in the string where the substring appears first, or, if not, returns a negative System.out.println ("Sb.indexof (\ is\") = "+ Sb.indexof (" is "));
Sets the parameter to the IndexOf method, specifying the starting position of the match System.out.println ("Sb.indexof (\ is\") = "+ Sb.indexof (" is ", 3);
Returns the last occurrence of a substring in a string, if it does not exist, returns a negative System.out.println ("Sb.lastindexof (\ is\") = "+ Sb.lastindexof (" is ")); Sets the parameters for the LastIndexOf method, specifying the matching end position System.out.println ("Sb.lastindexof (\ is\", 1) = "+ Sb.lastindexof (" is ", 1)
;
/** * Intercept 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, specifying the starting and ending positions of the interception System.out.print ("sb.substring (4,9) =" + sb.substring (4, 9));
}
/** * Get the character of a position in the string/public static void Testcharatstr () {stringbuffer sb = new StringBuffer (' This is a string
Buffer ");
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 ("Thi
S 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 ()); /** * Inserts various types of data into the string/public static void Testinsert () {stringbuffer sb = new StringBuffer (' This
is a stringbuffer! ");
The ability to insert characters, array of characters, strings, and various numeric and Boolean values Sb.insert (2, ' W ') at the specified position;
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 the string/public static void Testreplace () {stringbuffer sb = new StringBuffer (' This is
A stringbuffer! ");
Replaces a segment of a character in a string with another string Sb.replace (Sb.length (), "Integer");
System.out.println ("Testreplace:" + sb.tostring ()); /** * Reverse string/public static void Reversestr () {stringbuffer sb = new StringBuffer (' This is a Str
Ingbuffer! "); System.out.println (Sb.reverse ());
Reverse method will string backwards}}
Summary:
StringBuffer is not a invariant class, the new object is not created when the string content is modified, so it is more appropriate to modify the string than the string class;
The StringBuffer class does not provide the same ToCharArray method as String;
The Replace method of the StringBuffer class differs from the Replace method of the string class, whose replace method has three parameters, the first parameter specifies the starting position of the substituted substring, the second parameter specifies the ending position of the substituted substring, and the third parameter specifies the new substring.
The above is the entire content of this article, I hope to help you learn.