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

4
  • 10
    Please see stackoverflow.com/questions/6983856/…. It shows why the tokenizer will not work for a multi-character delimiter and shows alternatives (such as String.split(). Commented Aug 12, 2011 at 6:25
  • @Ray: I think that is correct and a good answer. Commented Aug 12, 2011 at 6: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 ");" Commented Aug 12, 2011 at 6: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. Commented Aug 12, 2011 at 8:00

6 Answers 6

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("\\);");
Sign up to request clarification or add additional context in comments.

Comments

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, ");" );

3 Comments

No, StringTokenizer is not deprecated! Common mistake, though!
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. "
Yes, exactly right, that is the source of the "mistake". Withdrawn. Little-d deprecated we can say!
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

Comments

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.

Comments

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++;
    }



}

}

Comments

0

Anything wrong with this?

String temp[] = str.split("\\);");

1 Comment

It gives regex.PatternSyntaxException. split() takes regex expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.