1

I have recently started learning Java. I got stuck on Splitting the particular string. Here is the string:

String head = "(*, grandparent(X,Y))";

I want to split the string such that it will give two tokens. The two tokens should be * and grandparent(X,Y). I have tried to split it by

StringTokenizer st=new StringTokenizer(head,",");
System.out.println("The tokens are: " + st.countTokens());

But I am getting three tokens if I split it by comma.

I don't want to split it by regex. Could you guys please help me?

5
  • If you always have 2 tokens you can specify the limit for number of tokens generated with String.split Commented Mar 22, 2016 at 0:46
  • 1
    Short answer based on title: Don't. Longer answer: Don't use StringTokenizer. As the javadoc has been saying since Java 1.4: 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. Commented Mar 22, 2016 at 0:49
  • Well, you are kinda supposed to get 3 strings if you split on ,, no? Commented Mar 22, 2016 at 0:49
  • and split takes parameters. which might be useful :). EDIT: Oops @Chris beat me to it :p Commented Mar 22, 2016 at 0:56
  • I realized my comment should be an answer... Expanded on it below. Commented Mar 22, 2016 at 0:57

4 Answers 4

1

If you always have 2 tokens you can specify the limit for number of tokens generated with String.split

For example: String[] tokens = head.split(",", 2)

Please don't use StringTokenizer in new code, its usage has been discouraged for a while in favor of newer better ways of doing similar work.

Sign up to request clarification or add additional context in comments.

Comments

1

You could find the first comma using indexOf(',').

Example:

String head="(*, grandparent(X,Y))";

int idx = head.indexOf(',');

String sub1 = head.substring(1, idx);
String sub2 = head.substring(idx + 1, head.length() - 1);

System.out.println("sub1 = " + sub1);
System.out.println("sub2 = " + sub2);

1 Comment

Pretty Useful! Thanks Zack.
1

You can use the Splitter from guava as following:

String head="(*, grandparent(X,Y))";

Iterable<String> tokens = Splitter.on(",").limit(2).split(head);

for(String token : tokens){
    System.out.println(token);
}

Below the maven dependency to add to your pom.xml

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>

Comments

-1

Hi I suggest to try this piece of code. It's a hack that works, depending on the content of the head string. For the shared value of head, it will work.

head=head.replace("*,","*;");
StringTokenizer st=new StringTokenizer(head,";");

Good luck

2 Comments

This really doesn't look like good style; quite a tacky hack.
@Debosmit Ray I agree it's a hack.

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.