We all know that the StringTokenizer class and split method in String class will break the string into multiple tokens for the given delimiter .we will be thinking which one to use as both functionality are same.But there is a feature which makes split more advantageous than StringTokenizer.
Lets take a scenario where we have a string("This,is,,Test") and we need to break this string into tokens separated by a delimiter comma(,).
String Tokenizer:
First we will use the StringTokenizer to split this string as shown below
The output for this will be
Token-This
Token-is
Token-Test
As we can see in the above output, StringTokenizer discarded the tokens which are empty string between the delimiter and hence the number of tokens printed is only 3.
Split:
Now we will split the string using split method as shown below
The output for this will be
Token-This
Token-is
Token-
Token-Test
As we can see in the above output, split takes care of tokens which are empty string between the delimiter and hence the number of tokens printed is 4.
Thus Split method is more useful in the scenarios where the string to be split'ted can have empty spaces between the delimiter.
Lets take a scenario where we have a string("This,is,,Test") and we need to break this string into tokens separated by a delimiter comma(,).
String Tokenizer:
First we will use the StringTokenizer to split this string as shown below
StringTokenizer st = new StringTokenizer("This,is,,Test",","); while (st.hasMoreTokens()) { System.out.println("Token-"+st.nextToken()); } |
The output for this will be
Token-This
Token-is
Token-Test
As we can see in the above output, StringTokenizer discarded the tokens which are empty string between the delimiter and hence the number of tokens printed is only 3.
Split:
Now we will split the string using split method as shown below
String[] strarray = ("This,is,,Test",").split(","); for (String test:strarray) System.out.println("Token-"+test); |
The output for this will be
Token-This
Token-is
Token-
Token-Test
As we can see in the above output, split takes care of tokens which are empty string between the delimiter and hence the number of tokens printed is 4.
Thus Split method is more useful in the scenarios where the string to be split'ted can have empty spaces between the delimiter.
No comments:
Post a Comment