1

I am using String Tokenizer in my program to separate strings. Delimiter I am trying to use is ");". But I found out that StringTokenizer uses ) and ; as 2 different delimiters. But I want to use it as combined. How can I do it?

my code:

StringTokenizer st = new StringTokenizer(str,");");
String temp[] = new String[st.countTokens()];
while(st.hasMoreTokens()) { 
    temp[i]=st.nextToken();
    i++;
}

Thanks

Gaurav
  • 1,700
  • 4
  • 22
  • 38
  • 10
    Please see http://stackoverflow.com/questions/6983856/why-is-stringtokenizer-deprecated. It shows why the tokenizer will not work for a multi-character delimiter and shows alternatives (such as `String.split()`. – Ray Toal Aug 12 '11 at 06:25
  • @Ray: I think that is correct and a good answer. – Kowser Aug 12 '11 at 06:37
  • Ok I know now that I will have to use String.split() but it takes regex expression which I am not familiar with. Can you help me what regex exp I can use for ");" – Gaurav Aug 12 '11 at 06:45
  • Since ), ; are special characters, for them to be interpreted as normal characters, you can done by preceding them with a defined escape character, usually the backslash "\". String[] result = str.split("\\)\\;"); ll do the work. – Swagatika Aug 12 '11 at 08:00

6 Answers6

2

As an alternative to String#split (StringTokenizer is deprecated), if you like Commons Lang, there is StringUtils#splitByWholeSeparator (null-safe, and no need to mess with regular expressions):

 String temp[] = splitByWholeSeparator(str, ");" );
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • No, StringTokenizer is not deprecated! Common mistake, though! – Ray Toal Aug 12 '11 at 06:39
  • Well, not @Deprecated, but "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. " – Thilo Aug 12 '11 at 06:41
  • Yes, exactly right, that is the source of the "mistake". Withdrawn. Little-d deprecated we _can_ say! – Ray Toal Aug 12 '11 at 06:43
2

As many of the answers have suggested, String.split() will solve your problem. To escape the specific sequence you're trying to tokenize on you will have to escape the ')' in your sequence like this:

str.split("\\);");
Jason Gritman
  • 5,251
  • 4
  • 30
  • 38
0

Anything wrong with this?

String temp[] = str.split("\\);");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You should try with the split(String regex) method from the String class. It should work just fine, and I guess it returns an array of Strings ( just like you seem to prefer). You can always cast to a List by using Arrays.asList() method.

Cheers, Tiberiu

jumping-jack
  • 237
  • 1
  • 3
0

"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."

Thats what Sun's doc says.

String[] result = "this is a test".split("\\s");

Is the recommended way to tokenize String.

Swagatika
  • 3,376
  • 6
  • 30
  • 39
0

This will work for you.

import java.util.StringTokenizer;


public class StringTest {

/**
 * @param args
 */
public static void main(String[] args) {
    int i = 0;
    String str = "one);two);three);four";
    StringTokenizer st = new StringTokenizer(str, ");");
    String temp[] = new String[st.countTokens()];

    while (st.hasMoreTokens()) {

        temp[i] = st.nextToken();
        System.out.println(temp[i]);
        i++;
    }



}

}