dcsimg
StringTokenizer
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Srinivas_Prabhu
Posted On:   Friday, February 11, 2005 03:17 AM

String p_sStr="This$$is$a test";
String p_sToken= "$$" ;


StringTokenizer st = new StringTokenizer(p_sStr, p_sToken);
String[] arrszTokens = new String[st.countTokens()];

int iTokenCount = 0;
while(st.hasMoreTokens()){

// arrszTokens[iTokenCount] = st.nextToken();

System.out.println(st.nextToken());

The output is
This
is
a
test

It is not taking $$ as a Combined String. How to do this ?

Re: StringTokenizer

Posted By:   Stephen_Ostermiller  
Posted On:   Monday, February 14, 2005 03:15 AM

StringTokenizer only does single character delimiters. It takes a string of single characters to use as delimiters as its argument. When you say "$$" you are actually saying, "Use $ as a delimiter, oh and use $ as delimiter". Not exactly what you mean. The second $ is completely redundant.


There are a couple things you can do. If you have Java 1.4 or later you can use the String.split method.


// Escape $ because it has special meaning in regular expressions.
String[] tokens = "This$$is$a test".split("\$\$");


If you are still using 1.3 or you want something a little more efficient you can use my StringHelper class. It has a split method that does not use regular expressions:


String[] tokens = com.Ostermiller.util.StringHelper.split("This$$is$a test", "$$") ;
About | Sitemap | Contact