Ok, I did a little sniper that does what you want, and something else. since you did not specify any more conditions, I did not experience the problem. I know this is a dirty way, and you can probably get better results with what has already been done. but for the pleasure of programming, here is an example:
String example = "hello\"John Smith\" Ted Barry lol\"Basi German\"hello"; int wordQuoteStartIndex=0; int wordQuoteEndIndex=0; int wordSpaceStartIndex = 0; int wordSpaceEndIndex = 0; boolean foundQuote = false; for(int index=0;index<example.length();index++) { if(example.charAt(index)=='\"') { if(foundQuote==true) { wordQuoteEndIndex=index+1; //Print the quoted word System.out.println(example.substring(wordQuoteStartIndex, wordQuoteEndIndex));//here you can remove quotes by changing to (wordQuoteStartIndex+1, wordQuoteEndIndex-1) foundQuote=false; if(index+1<example.length()) { wordSpaceStartIndex = index+1; } }else { wordSpaceEndIndex=index; if(wordSpaceStartIndex!=wordSpaceEndIndex) { //print the word in spaces System.out.println(example.substring(wordSpaceStartIndex, wordSpaceEndIndex)); } wordQuoteStartIndex=index; foundQuote = true; } } if(foundQuote==false) { if(example.charAt(index)==' ') { wordSpaceEndIndex = index; if(wordSpaceStartIndex!=wordSpaceEndIndex) { //print the word in spaces System.out.println(example.substring(wordSpaceStartIndex, wordSpaceEndIndex)); } wordSpaceStartIndex = index+1; } if(index==example.length()-1) { if(example.charAt(index)!='\"') { //print the word in spaces System.out.println(example.substring(wordSpaceStartIndex, example.length())); } } } }
it also checks for words that were not separated by a space after or before quotation marks, such as the words "hello" before "John Smith" and after "Basi German".
when the line is changed to "John Smith" Ted Barry , the output consists of three lines, 1) "John Smith" 2) Ted 3) Barry
The line in the example welcomes "John Smith" Ted Barry Lol "Bazi German" hello and prints 1) Hello 2) "John Smith" 3) Ted 4) Barry 5) lol 6) "Bazi German" 7) hello
Hope this helps
Basilio german
source share