-1

I am trying to tokenize strings which includes quotes. I searched and found StringTokenizer class. But it didn't worked for me. Here is an example of the string I expect user to enter:

dgdfgg ddfgdf 4  12 assdsd “michael smith” “michael Westley” “Michael Fotky”

Problem is tokenizer tokenizes words. But as you see there is quotes and I want them to stay together. Like “michael Westley” should stay together as a token. Not like “michael and Westley”. I could use str.nextToken()+str.nextToken() But Idont know if string inside the quotes will be one word or two words. If it is one word it could be a problem. And Here is a example of what I want:

dgdfgg 
ddfgdf 
4
12 
assdsd 
“michael smith”
“michael Westley”
“Michael Fotky”

Thanks In advance.

ossobuko
  • 781
  • 7
  • 24
  • 1
    Check out [String.split()](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)... – jahroy Apr 04 '14 at 15:18

1 Answers1

1

If you want something elegant you should look on regex possibilities. But to do it quickly you can just check your outputs and concatenates your strings that are included between '"'. For example :

String token = str.nextToken();
if(token.charAt(0) == '"' && token.charAt(token.length()-1) != '"') {
 String tmp = str.nexToken();
 while(tmp.charAt(tmp.length()-1) != '"') {
  token += tmp;
  tmp = str.nextToken();
 }
}
// do something with token
...

Hope it helped

BaptisteL
  • 450
  • 4
  • 13