The StringBuffer class provides a variable sequence of strings, similar to the String class, but it can be modified at will to store characters and is much more flexible to use than the String class. Its commonly used constructor is:
StringBuffer()
Construct an empty StringBuffer object with an initial capacity of 16 characters.
StringBuffer(Stringstr)
Construct a StringBuffer object with the initial content as a copy of the string str.
For the StringBuffer class, in addition to the commonly used methods such as length, string interception, and string retrieval that can be used in the String class, there are two more convenient method series, namely the append method series and the insert method series.
(1) The append method series directly adds data at the end of the StringBuffer object according to the data type of the parameter.
public StringBuffer append(boolean b)public StringBuffer append(char c)public StringBuffer append(char[] str)public StringBuffer append(char[] str, int offset, int len)public StringBuffer append(double d)public StringBuffer append(float f)public StringBuffer append(int i)public StringBuffer append(long l)public StringBuffer append(Object obj)public StringBuffer append(String str)public StringBuffer append(StringBuffer sb)
(2) The insert method series inserts data at the offset position of the StringBuffer according to the data type of the parameter.
public StringBuffer insert(int offset, boolean b)public StringBuffer insert(int offset, char c)public StringBuffer insert(int offset, char[] str)public StringBuffer insert(int index, char[] str, int offset, int len)public StringBuffer insert(int offset, double d)public StringBuffer insert(int offset, float f)public StringBuffer insert(int offset, int i)public StringBuffer insert(int offset, long l)public StringBuffer insert(int offset, Object obj)public StringBuffer insert(int offset, String str)
(3) The following method is used to convert the data of the stringbuffer object into a string:
public String toString()
[Example 3.12] Modify based on Example 3.11, and use the StringBuffer object to obtain the output interface as shown in Figure 3.10.
//The program file name is TestString.java public class TestString { public static void main(String[] args) { StringBuffer str = new StringBuffer("The substring begins at the specified beginIndex."); StringBuffer str1 = new StringBuffer("string"); String str2 = new String(); int size = str.length(); int flag = str.indexOf("substring"); str2 = str.substring(flag,flag + 9); StringBuffer strOut = new StringBuffer("String"); strOut.append(str); strOut.append("Total length is:"); strOut.append(size); int f = strOut.indexOf("Total"); strOut.insert(f,'/n'); System.out.println(strOut.toString()); if(str1.toString().equals(str2)) System.out.println("Intercepted string is: " + str1.toString()); else System.out.println("Intercepted string is: " + str2); } }StringTokenizer(Stringstr,Stringdelim)
Use the delim delimiter to build the StringTokenizer object as the initial string str.
intcountTokens()
Returns the total number of identified marks.
booleanhasMoreTokens()
Test whether there are still signs for identification.
booleannextToken(Stringdelim)
Returns the next token separated by the string delim.
StringnextToken()
Returns the next identified mark.
import java.util.*; public class UseToken { public static void main(String[] args) { String str = "Mathematics::English::Chinese::Chemistry"; StringTokenizer st = new StringTokenizer(str,"::"); System.out.println(str + "/nThe number of courses is: " +st.countTokens()); while (st.hasMoreTokens()) { System.out.println(st.nextToken("::")); } str = "Hello this is a test"; st = new StringTokenizer(str); System.out.println(str + "/n word count is: " +st.countTokens()); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }Summarize
The above is all the content of this article about the code analysis of JAVA StringBuffer class and StringTokenizer class. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!