JAVA StringBuffer class and StringTokenizer class code parsing

  • 2020-12-10 00:42:17
  • OfStack

The & # 160; The StringBuffer class provides a mutable sequence of 1 string, similar to the String class, but it can modify the stored sequence of characters arbitrarily and is much more flexible to use than the String class. Its common constructors are:

StringBuffer()

Construct an empty StringBuffer object with an initial capacity of 16 characters.

StringBuffer(Stringstr)

Construct 1 StringBuffer object, starting with a copy of the string str.

For the StringBuffer class, in addition to the methods of image length, string interception and string retrieval commonly used in the String class, there are two more convenient method families, namely the append method family and the insert method family.

(1) The append method series adds data directly 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 carries out data insertion at the offset position of 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 StringBuffer object to get the output interface as shown in Figure 3.10.


// Program file name 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(" The total length is: ");    
  strOut.append(size); 
  int f = strOut.indexOf(" The total "); 
  strOut.insert(f,'\n'); 
  System.out.println(strOut.toString());  
  if(str1.toString().equals(str2)) 
     System.out.println(" The intercepted string is: " + str1.toString()); 
  else 
     System.out.println(" The intercepted string is: " + str2); 
    } 
} 

StringTokenizer(Stringstr,Stringdelim)  

Use the delim delimiter to build the StringTokenizer object from the initial string str. The & # 160;

intcountTokens()  

Returns the total number of tokens recognized. The & # 160;

booleanhasMoreTokens()  

Test if there is any recognition mark. The & # 160;

booleannextToken(Stringdelim)  

Returns the next token delimited by the string delim. The & # 160;

StringnextToken()  

Returns the next recognized token. The & # 160;


import java.util.*; 
 
public class UseToken  
{ 
  public static void main(String[] args)  
  { 
    String str = " mathematics :: English :: Chinese language and literature :: chemical "; 
    StringTokenizer st = new StringTokenizer(str,"::"); 
    System.out.println(str + "\n The 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 Number of words: " +st.countTokens()); 
  while (st.hasMoreTokens())  
  { 
    System.out.println(st.nextToken()); 
      } 
   } 
} 

conclusion

That is the end of this article on JAVA StringBuffer and StringTokenizer class code analysis, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: