String class:
The String class represents strings. All string literals in a Java program are implemented as instances of this class. Strings are constants, and their values cannot be changed after they have been created. A string buffer supports variable strings. Because the String object is immutable,
can be shared.
The string class includes methods that can be used to examine a single character of a sequence, compare strings, search strings, extract substrings, create a copy of a string, and convert all characters to uppercase or lowercase. The Java language provides string concatenation symbols ("+") and other
Special support for converting objects to strings. string concatenation is implemented through the StringBuilder (or StringBuffer) class and its append methods. String conversions are implemented by the ToString method, which is defined by the Object class and can be used by the Java
Inherits from all classes in the.
The split method of the string class splits the string according to the matching of the given regular expression, so that the string can be separated into a single character form.
StringTokenizer class:
The string Tokenizer class allows an application to decompose a string into tokens. The Tokenization method is simpler than the method used by the Streamtokenizer class. The StringTokenizer method does not distinguish between identifiers, numbers, and quoted strings, and they do not recognize and
Skips comments. You can specify it at creation time, or you can specify a delimiter (delimited character) set based on each tag.
An instance of StringTokenizer behaves in two ways, depending on whether the value of the RETURNDELIMS flag it uses at creation time is true or false:
If the flag is false, the delimiter character is used to delimit the tag. The tag is the largest sequence of consecutive characters (not delimiters).
If the flag is true, those delimiter characters are considered to be tokens themselves. So the tag is either a delimiter character or the maximum sequence of consecutive characters (not delimiters).
The StringTokenizer object is internally maintained in the string to be marked in the current position. Some actions move this current position to the processed word specifier. Returns a token by intercepting a substring of the string used to create the StringTokenizer object.
How to convert a comma-separated string into an array
Package com.test.string;//Build Your own packages
Import java.util.stringtokenizer;//imports the StringTokenizer class, which is followed by this class to create objects and methods of this class.
public class strtoarray{
public static void Main (string[] args) {
String str= "A,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
String[] Strarray=null;
System.out.println ("Call Convertstrtoarray Result:");
Strarray = Convertstrtoarray (str);
PrintArray (Strarray);
System.out.println ("Call ConvertStrToArray2 Result:");
Strarray = ConvertStrToArray2 (str);
PrintArray (Strarray);
}
Using the Split method of string
public static string[] Convertstrtoarray (String str) {
string[] Strarray = null;
Strarray = Str.split (","); Split the character to "," and then give the result to the array strarray
return strarray;
}
Using the StringTokenizer implementation
API Description: StringTokenizer is a legacy class that is retained for compatibility reasons
(although it is not encouraged in the new code). It is recommended that all people seeking this function use
The split method of String or the Java.util.regex package
public static string[] ConvertStrToArray2 (String str) {
StringTokenizer st = new StringTokenizer (str, ",");//Put "," as a partition flag, and then assign the well-divided character to the StringTokenizer object.
string[] Strarray = new String[st.counttokens ()];//evaluates the StringTokenizer method of the Counttokens class to call this tokenizer before generating an exception The number of NextToken methods.
int i=0;
while (St.hasmoretokens ()) {//See if there are more available tags in this tokenizer string.
strarray[i++] = St.nexttoken ();//Returns the next token of this string tokenizer.
}
return strarray;
}
Output array
public static void PrintArray (string[] array) {
for (int i=0;i<array.length;i++) {
System.out.print (Array[i]);
if (i==array.length-1) {
System.out.print ("\ n");
}else{
System.out.print (",");
}
}
}
}
Although this kind of use is not many, the method is easy to grasp, but it is necessary to understand.
Java converts a comma-separated string into an array