Here is the situation:
I have a String and I want to get tokens from it. I have 3 delimiters which are:
" " (space)
"," (comma)
";" (semi colon)
Is there a way to use StringTokenizer with these 3 demiliters?
Please look into this.
Thanks in advance.
Printable View
Here is the situation:
I have a String and I want to get tokens from it. I have 3 delimiters which are:
" " (space)
"," (comma)
";" (semi colon)
Is there a way to use StringTokenizer with these 3 demiliters?
Please look into this.
Thanks in advance.
Code:String s = "The StringTokenizer methods do not distinguish " +
"among identifiers, numbers, and quoted strings, " +
"nor do they recognize and skip comments; it is " +
"a legacy class.";
System.out.println("s = " + s);
String delims = " ,;";
StringTokenizer tokens = new StringTokenizer(s, delims);
while(tokens.hasMoreTokens())
System.out.println(tokens.nextToken());
Perfect. It worked. Thanks.
I have another question. What if my token is composed of more than one characters?
Output:Code:String s= "I am new to Java. Are you good in Java, if yes please help me.";
String delim = "Java";
StringTokenizer tokens = new StringTokenizer(s, delim);
while(tokens.hasMoreTokens())
System.out.println(tokens.nextToken());
Actually "Java" is not treated as a token but 4 different tokens (each character is a token). How to handle this?Code:Welcome
I
m new to
. Are you good in
, if yes ple
se help me.