Extract 1st and 2nd tokens..

Hi,

Let's assume that I have a string, like so:

 String s = "123 A 456 B";

the String will always be in that format. I need to tokenize it, so that I have two Strings, one with "123 A" and the other with "456 B".

What's the best way to achieve this?

Is there a way to specify a delimiter (perhaps with String#split() or StringTokenizer) but specify for it to ignore certain delimiters?? :o\ If you see what I mean..?

For example:

  Tokenize String; with delimiter as " "; but don't process the first and last delimiters.

Cheers.
LVL 25
InteractiveMindAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Or rather

String s1 = toks[0] + " " +  toks[1];
0
 
CEHJCommented:
String[] toks = s.split(" ");
String s1 = toks[0] + toks[1];
0
 
aozarovCommented:
in Both stringTokenizer and split you define the delimters (all other characters will be used as regular characters).
What is the delimter in this case? is it a split based constant length?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
InteractiveMindAuthor Commented:
Cheers CEHJ..

I have another, related question: I've seen you Java pros using all these braces, and comma-delimited numbers with the split() method, here's me making one up (probably not valid):

  String s2 = s1.split( {1,2} );   // Can't really remember what I've seen exactly before, but do you know what I mean?

If so, then can you explain what they're for, and how to use them?

Thanks.
0
 
CEHJCommented:
I think you're taling about regexes. They're to define repetition operators. See

http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

api notes
0
 
InteractiveMindAuthor Commented:
Cheers  :-)
0
 
CEHJCommented:
:-)
0
 
aozarovCommented:
I might have not understand your question
But I am not sure why "String s1 = toks[0] + " " +  toks[1];" solve your problem
As this will just concatinate "123" " " and "A"
What if you have different delimiter to be ignored?
What about ignoring the last delimiter?
What about getting all the rest of the tokens?

As I though your question was: parse with delimiter as " "; but don't process the first and last delimiters.
0
 
InteractiveMindAuthor Commented:
aozarov,

> As I though your question was: parse with delimiter as " "; but don't process the first and last delimiters.
Yes, it was.. but I got the impression that CEHJ's solution was the best way to go about it..?
0
 
aozarovCommented:
I thought such basic operation (which does not solve your problem) was already known to you as you mentioned "String#split() or StringTokenizer".
I am not trying to tell you what to do with your points :-)
I just wanted to make sure that you do understand that your solution was not solved yet (and I can see you already know that).
Good luck ;-)
0
 
InteractiveMindAuthor Commented:
Ah right. I'm with you.  :-)  Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.