How do I split string using String.split() without having trailing/leading spaces or empty values?
Let’s say I have string such as " ol-ga@assf-rrt.ru ; sdf.an@dfgdfg.com; sdfsdf@fdfd.erff, privet@vvv.fff ".
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}" ol-ga@assf-rrt.ru"
[1] = {java.lang.String@98}"sdf.an@dfgdfg.com"
[2] = {java.lang.String@99}"sdfsdf@fdfd.erff"
[3] = {java.lang.String@100}"privet@vvv.fff "
Is it possible to get array of “clean” emails using only regex and Split (without using extra call to String.trim()) ?
Thanks!
I’m assuming that you’re pretty sure your input string contains nothing but email addresses and just need to trim/reformat.