|
|
|
|
The
|
package inputOutput;
import java.util.StringTokenizer;
public class TokenizeString
{
public static void main(String[] args)
{
StringTokenizer st = new StringTokenizer("Java is great!");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
} |
which prints out:
Java is great!
Note that the default delimiter characters are:
Here's another, modified, version, where we specify the delimiters explicitly:
package inputOutput;
import java.util.StringTokenizer;
public class TokenizeString
{
public static void main(String[] args)
{
String delimiters = ".;!?;:";
String paragraph =
"The spirit of Java is alive in the world! " +
"How can you be ignorant of it? " +
"Parse, compile, and assimilate; " +
"perhaps the world will become your oyster! " +
"An example: 'Write once, run anywhere.'";
StringTokenizer st =
new StringTokenizer(paragraph, delimiters);
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
} |
It outputs the following:
The spirit of Java is alive in the world How can you be ignorant of it Parse, compile, and assimilate perhaps the world will become your oyster An example 'Write once, run anywhere '
Note that, in the above example, we are considering tokens to be sentences (well, kind of -- actually, we're just using the punctuation as delimiters). In other words, tokens do not have to be words, or single characters only. A token may be any sequence of characters.
If we want the delimiter characters also to be returned as tokens, we can make a slight further modification:
package inputOutput;
import java.util.StringTokenizer;
public class TokenizeString
{
public static void main(String[] args)
{
String delimiters = ".;!?;:";
String paragraph =
"The spirit of Java is alive in the world! " +
"How can you be ignorant of it? " +
"Parse, compile, and assimilate; " +
"perhaps the world will become your oyster! " +
"An example: 'Write once, run anywhere.'";
StringTokenizer st =
new StringTokenizer(paragraph, delimiters, true);
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
} |
which outputs:
The spirit of Java is alive in the world ! How can you be ignorant of it ? Parse, compile, and assimilate ; perhaps the world will become your oyster ! An example : 'Write once, run anywhere . '
Perhaps we'd like to handle things inside single quotes a little better:
package inputOutput;
import java.util.StringTokenizer;
public class TokenizeString
{
public static void main(String[] args)
{
String delimiters = ".;!?;:'"; // Added single quote
String paragraph =
"The spirit of Java is alive in the world! " +
"How can you be ignorant of it? " +
"Parse, compile, and assimilate; " +
"perhaps the world will become your oyster! " +
"An example: 'Write once, run anywhere.'";
StringTokenizer st =
new StringTokenizer(paragraph, delimiters, true);
while (st.hasMoreTokens())
{
String token = st.nextToken();
if (token.equals("'"))
{
if (st.hasMoreTokens())
token += st.nextToken("'"); // Use different delimiter set.
if (st.hasMoreTokens())
token += st.nextToken();
}
System.out.println(token);
}
}
} |
which outputs:
The spirit of Java is alive in the world ! How can you be ignorant of it ? Parse, compile, and assimilate ; perhaps the world will become your oyster ! An example : 'Write once, run anywhere.'
| The page was last updated February 19, 2008 |