The substring method in Java can decompose a string and return a substring of the original string. If you want to tell a string to be broken down into one word or tag, stringtokenizer can help you.
int Counttokens (): Returns the number of times the Nexttoken method was called.
Boolean hasmoretokens (): Returns whether there is a delimiter.
Boolean hasmoreelements (): Returns whether there is a delimiter.
String NextToken (): Returns the string from the current position to the next delimiter.
Object nextelement (): Returns the string from the current position to the next delimiter.
String NextToken (String Delim): Similar to 4, returns the result with the specified delimiter.
Let's look at an example:
Public Static void New StringTokenizer ("www.ooobj.com", ". B"while(St.hasmoretokens ()) { System.out.println ("Token:" + st.nexttoken ());}}
Output:
Token:www
Token:ooo
Token:j
Token:com
There are two common methods of StringTokenizer:
1.hasMoreElements (). This method is the same as the use of the Hasmoretokens () method, except that the method implemented by StringTokenizer to implement the enumeration interface can be seen from the StringTokenizer declaration: Class StringTokenizer implements Enumeration<object>.
2.nextElement (). This method is the same as the Nexttoken () method, and returns the next token for this stringtokenizer.
Three construction methods for StringTokenizer:
1.StringTokenizer (String str). The default is "\t\n\r\f" (there is a space before, the quotation marks are not) as the delimiter.
Source:
Public StringTokenizer (String str) {
This (str, "\t\n\r\f", false);
}
Instance:
Public Static void New stringtokenizer ("www ooobj com"while(St.hasmoreelements ()) { System.out.println ("Token:" + st.nexttoken ());}}
Output:
Token:www
Token:ooobj
Token:com
2.StringTokenizer (String str, string delim). Specify Delim as the separator, see the first example.
3.StringTokenizer (String str, String Delim, Boolean returndelims). Returndelims is true, the Delim delimiter is also considered a marker.
Instance:
Public Static void Main (string[] args) { newtrue); while (St.hasmoreelements ()) { System.out.println ("Token:" + st.nexttoken ()); }
Output:
Token:www
Token:.
Token:ooobj
Token:.
Token:com
Java string decomposition StringTokenizer usage (higher efficiency than the split () method)