[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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.
0
InteractiveMind
Asked:
InteractiveMind
  • 4
  • 4
  • 3
1 Solution
 
CEHJCommented:
String[] toks = s.split(" ");
String s1 = toks[0] + toks[1];
0
 
CEHJCommented:
Or rather

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now