java - Dividing a string by spaces except when a specific character is met -


i have program takes in string , stores in string array. before doing this, divides string spaces , stores each index of array.

however, want string not separate space if character, such quotation marks, present. long string inside of quotation mark, should store entire substring in string of own in array, if has spaces inside. make more clear, here command parses:

addgame 1001 "mirror's edge" 

where first part command used switch switch case statements have. second game id , third name of game. far, when make game class , print out, returns:

(1001, "mirror's) 

while should be:

(1001, mirror's edge) 

i don't want use regex if possible. can point me in right direction?

the "easy" way:

string[] array = str.split("\"?, *\"?(?=(([^\"]*\"){2})*[^\"]*$"); 

the "hard" way (pseudo code):

  • work way along input character character
  • count number of quotes encountered
  • if number odd, not treat commas break points
  • ignore quotes purposes of collecting parts
  • ignore number of spaces after breaking commas

the regex version that.

although regex seems "hard", amount of time , debugging spent implementing manually makes non-regex way "hard".


Comments