string tokenizer java

‮‬https://www.lautturi.com
string tokenizer java

To split a string into an array of tokens in Java, you can use the StringTokenizer class. This class allows you to specify a delimiter string and use it to tokenize the input string.

Here's an example of how to use the StringTokenizer class to split a string into tokens:

String s = "Hello, world!";
StringTokenizer st = new StringTokenizer(s, ",");
while (st.hasMoreTokens()) {
    String token = st.nextToken();
    System.out.println(token);
}

In this example, we're using the "," string as the delimiter, so the output will be:

Hello
 world!

You can use a different delimiter string by specifying it as the second argument to the StringTokenizer constructor. You can also use multiple delimiter strings by specifying them as a string containing all the delimiter characters, for example:

String s = "Hello, world!";
StringTokenizer st = new StringTokenizer(s, " ,!");
while (st.hasMoreTokens()) {
    String token = st.nextToken();
    System.out.println(token);
}

This will split the input string at each occurrence of a comma, space, or exclamation point, and the output will be:

Hello
world

You can find more information about the StringTokenizer class and examples of how to use it in the Java documentation.

Created Time:2017-10-17 20:18:55  Author:lautturi