Parsing comma delimited text in Java -


If I have an ArrayList that is similar to the lines of data that can appear:

 < Code> bob, jones, 123-333-1111 James, Lee, 234-333-2222  

How do I remove extra whitespace and get back the same data? I thought that you could spit the string using "," and then trim (), but I did not know what the syntax would be or how to apply it, thinking that it was a good way to do it Because I want to put each field in an array, so in this case there is a [2] [3] array, and then put it back in the ArrayList after removing the white space. But it seems that there is a strange way to do this, and if there is a change in my list then the chances of not having an email at intervals increased. any idea? Thank you.

EDIT: Dumber question, so I still can not confirm how I can process data, because I can not do this right:

 For  ( String S: myList) {String st [] = s.split (", \\ s *"); }  

Since the St. [] will lose the scope after the forehead loop. And if I already declare string stat [], then I do not know how my array is right? Thank you.

You can scan through the entire string and create a new string, which Apart from leaving the white space, it will be more efficient after adding a split and then adding it again. Something like this should be done:

  string str = / * array from its original string / /; Stringbilder sb = new stringbiller (); Boolean Skip = True; For (int i = 0; i & lt; str.length (); i ++) {char ch = str.charAt (i); If (Skip and & amp; amp; character. Webspace (ch)) continues; Sb.append (ch); If (ch == ',') skip = true; Else skip = false; } String result = sb.toString ();  

Comments