dcsimg
Question about StringTokenizer working
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Seena_Mathew
Posted On:   Saturday, August 2, 2008 10:22 PM

I need to a split a string on basis of presence of some string.Since the pattern is getting interpreted in the browser as long dash when posting I'll write the symbol names ampersand(&)pound(#)151; String textToBeSplit = "Hello world—This is a String Tokenizer test—Individual Roth 401(k)"; So based on that I was expecting 3 tokens Token 1 - Hello world Token 2 - This is a String Tokenizer test Token 3 - Individual Roth 401(k) But when I ran the test i got 4 tokens Token 1 - Hello world Token 2 - This is a String Tokenizer test Token 3 - Individual Roth 40 Token 4 - (k)    More>>

I need to a split a string on basis of presence of some string.Since the pattern is getting interpreted in the browser as long dash when posting I'll write the symbol names
ampersand(&)pound(#)151;


String textToBeSplit = "Hello world—This is a String Tokenizer test—Individual Roth 401(k)";


So based on that I was expecting 3 tokens

Token 1 - Hello world

Token 2 - This is a String Tokenizer test

Token 3 - Individual Roth 401(k)



But when I ran the test i got 4 tokens

Token 1 - Hello world

Token 2 - This is a String Tokenizer test

Token 3 - Individual Roth 40

Token 4 - (k)


The code to tokenize was

			

public void tokenizeText(String textToBeSplit) {
// browser interpreting the pattern as long dash but in java code the pattern i'll be getting is
// ampersand(&)pound(#)151semicolon(;)

String splitPattern = "—";

StringTokenizer st = new StringTokenizer(textToBeSplit, splitPattern);
while (st.hasMoreTokens()){
String str = st.nextToken();
System.out.println("token by tokenize = "+str);
}
}



I did finally got 3 tokens by using split().
But I would like to know what was wrong with the above code and why did StringTokenizer not work properly in this case.
I have tried googling but have not found anything beyond StringTokenizer is now discouraged since its a legacy class.


Thanks    <<Less

Re: Question about StringTokenizer working

Posted By:   dd_dd  
Posted On:   Tuesday, August 26, 2008 02:57 AM

Hi,
I ran your program.
I Got result from your program like,

token by tokenize = Hello world
token by tokenize = This is a String Tokenizer test
token by tokenize = Individual Roth 401(k)


So,its executing perfectly.I am not sure where it's giving wrong.
About | Sitemap | Contact