Crystal
Crystal

Reputation: 29458

Parsing comma delimited text in Java

If I have an ArrayList that has lines of data that could look like:

bob,      jones,    123-333-1111
    james, lee,  234-333-2222

How do I delete the extra whitespace and get the same data back? I thought you could maybe spit the string by "," and then use trim(), but I didn't know what the syntax of that would be or how to implement that, assuming that is an ok way to do it because I'd want to put each field in an array. So in this case have a [2][3] array, and then put it back in the ArrayList after removing the whitespace. But that seems like a funny way to do it, and not scaleable if my list changed, like having an email on the end. Any thoughts? Thanks.

Edit: Dumber question, so I'm still not sure how I can process the data, because I can't do this right:

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

since st[] will lose scope after the foreach loop. And if I declare String st[] beforehand, I wouldn't know how big to create my array right? Thanks.

Upvotes: 2

Views: 2058

Answers (8)

Robert Watkins
Robert Watkins

Reputation: 2206

I've had a lot of success using this library.

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80176

you can use Sting.split() method in java or u can use split() method from google guava library's Splitter class as shown below

static final Splitter MY_SPLITTER = Splitter.on(',') .trimResults() .omitEmptyStrings();

Upvotes: 0

S73417H
S73417H

Reputation: 2671

Could be a bit more elegant, but it works...

ArrayList<String> strings = new ArrayList<String>();
strings.add("bob,      jones,    123-333-1111");
strings.add("james, lee,  234-333-2222");

for(int i = 0; i < strings.size(); i++) {
  StringBuilder builder = new StringBuilder();
  for(String str: strings.get(i).split(",\\s*")) {
    builder.append(str).append(" ");
  }
  strings.set(i, builder.toString().trim());
}

System.out.println("strings = " + strings);

Upvotes: 0

functional
functional

Reputation:

If I understood your problem, here is a solution:

    ArrayList<String> tmp = new ArrayList<String>();
    tmp.add("bob,      jones,    123-333-1111");
    tmp.add("    james, lee,  234-333-2222");

    ArrayList<String> fixedStrings = new ArrayList<String>();

    for (String i : tmp)    {
        System.out.println(i);
        String[] data = i.split(",");

        String result = "";
        for (int j = 0; j < data.length - 1; ++j)   {
            result += data[j].trim() + ", ";
        }

        result += data[data.length - 1].trim();

        fixedStrings.add(result);
    }

    System.out.println(fixedStrings.get(0));
    System.out.println(fixedStrings.get(1));

I guess it could be fixed not to create a second ArrayLis. But it's scalable, so if you get lines in the future like: "bob, jones , [email protected] , 123-333-1111 " it will still work.

Upvotes: 1

casablanca
casablanca

Reputation: 70701

You could just scan through the entire string and build a new string, skipping any whitespace that occurs after a comma. This would be more efficient than splitting and rejoining. Something like this should work:

String str = /* your original string from the array */;
StringBuilder sb = new StringBuilder();
boolean skip = true;

for (int i = 0; i < str.length(); i++) {
  char ch = str.charAt(i);

  if (skip && Character.isWhitespace(ch))
    continue;

  sb.append(ch);

  if (ch == ',')
    skip = true;
  else
    skip = false;
}

String result = sb.toString();

Upvotes: 5

Chris Dennett
Chris Dennett

Reputation: 22721

Just split() on each line with the delimiter set as ',' to get an array of Strings with the extra whitespace, and then use the trim() method on the elements of the String array, perhaps as they are being used or in advance. Remember that the trim() method gives you back a new string object (a String object is immutable).

Upvotes: 1

Dunderklumpen
Dunderklumpen

Reputation: 1874

If you use a regex for you split, you can specify, a comma followed by optional whitespace (which includes spaces and tabs just in case).

String[] fields = mystring.split(",\\s*");

Depending on whether you want to parse each line separately or not you may first want to create an array split on a line return

String[] lines = mystring.split("\\n");

Upvotes: 2

Related Questions