5

How do I split string using String.split() without having trailing/leading spaces or empty values?

Let's say I have string such as " [email protected] ; [email protected]; [email protected], [email protected] ".

I used to split it by calling String.split("[;, ]+") but drawback is that you get empty array elements that you need to ignore in extra loop.

I also tried String.split("\\s*[;,]+\\s*") which doesn't give empty elements but leaves leading space in first email and trailing space in last email so that resulting array looks like because there are no commas or semicolons next to those emails:

[0] = {java.lang.String@97}"  [email protected]"
[1] = {java.lang.String@98}"[email protected]"
[2] = {java.lang.String@99}"[email protected]"
[3] = {java.lang.String@100}"[email protected]  "

Is it possible to get array of "clean" emails using only regex and Split (without using extra call to String.trim()) ?

Thanks!

2
  • Why not do a String.trim() before adding the string to the array? Commented Apr 13, 2012 at 20:11
  • Because I'm not adding strings to the array, String.split does. Commented Apr 13, 2012 at 20:13

5 Answers 5

6
String input = " [email protected] ; [email protected]; [email protected], [email protected] ";
input = input.replace(" ", "");
String[] emailList = input.split("[;,]+");

I'm assuming that you're pretty sure your input string contains nothing but email addresses and just need to trim/reformat.

Sign up to request clarification or add additional context in comments.

4 Comments

I edited to put the string.replace() call on its own line; it was hidden at the end of the first, long line, making this answer's suggested solution not obvious at first.
Yes, I think replacing all spaces before splitting is the most elegant solution here.
More general space replacement would be: input.replaceAll("\\s+", "")
String replacement is much less efficient than a plain trim: it does create a new string instead of re-using the char[] from the original one.
3

Like this:

String.split("\\s*(;|,|\\s+)\\s*");

But it gives an empty string in the beginning (no way to get rid of it using only split).

Thus only something like this can help:

String.trim().split("\\s*(;|,)\\s*");

5 Comments

No luck, it gives same result like with '\\s*[;,]+\\s*'
BTW, I removed split. See my comment.
You probably meant "trim" :) New version of your pattern gives empty element at the beginning of resulting array. I begin to think it's not doable with pure regex :(
Right, I meant trim. Yes, no way to get rid of starting space with only regex.
Don't think you'll be able to do what you want under those restrictions mate. Have to remember your regex defines the split conditions (ie the stuff in-between your matches). Unless you don't mind having empty matches at the start and end of your array, there's no way to trim those using pure regex.
3

Just throwing it out there: if you use Guava, Splitter makes this a bit simpler than it is with regexes.

Iterable<String> splitStrings = Splitter
 .onPattern("[;,]+")
 .trimResults()
 .split(string);

Comments

1

Try using String.trim() in the result of the split

1 Comment

using only regex and Split :)
0

First of all, as DGomez pointed out:

 String.trim()

Use that in the input string, so the leading and trailing spaces are gone.

Now, use this regex for splitting the emails:

 String.split("[,;]+\\s*")

I think this should do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.