Reputation: 545
I am using the StringTokenizer in java to tokenize the string as shown in following is my code
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
String str = "key1@@@@@val1#####key2@@@@@val2#####key3@@@@@val3#####key4@@@@@val4###val4###val4#####key5@@@@@val5";
StringTokenizer st = new StringTokenizer(str,"#####");
while(st.hasMoreTokens()){
System.out.println(st.nextElement());
}
}
}
The output is
key1@@@@@val1
key2@@@@@val2
key3@@@@@val3
key4@@@@@val4
val4
val4
key5@@@@@val5
Actually the delimiter is "#####" so why it is breaking at "###"
Expected output
key1@@@@@val1
key2@@@@@val2
key3@@@@@val3
key4@@@@@val4###val4###val4
key5@@@@@val5
I know i can do it by using another delimiter than ### but I want to know exact reason behind this.
Upvotes: 0
Views: 522
Reputation: 842
If you want multiple delimiters to be used for breaking string, you can use the following as the example.
String msg = "http://192.173.15.36:8084/";
StringTokenizer st = new StringTokenizer(msg, "://.");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
More details here
Upvotes: 0
Reputation: 4360
The delimeter is a character which is not considered as a token, so also mentioning #
as delimeter it will give you the same output. Quoting from java docs
The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
Hence any character mentioned as delimeter will completely be ignored and the remaining String will be tokenised.
And quoting from same source as an better alternative for your purpose:-
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Use string.split("#####")
Upvotes: 2
Reputation: 18242
In StringTokenizer you specify the characters that are going to be used as delimiters.
Your line
StringTokenizer st = new StringTokenizer(str, "#####");
is equivalent to
StringTokenizer st = new StringTokenizer(str, "#");
To understand the case you can do
StringTokenizer st = new StringTokenizer(str, "#@");
Upvotes: 2
Reputation: 178421
The StringTokenizer
splits according to each of the characters in the provided delim
argument.
In your case, you generate a StringTokenizer
that splits according to '#'
only.
From the java docs of StringTokenizer(String,String)
.... The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens....
Upvotes: 3