How to split a string with whitespace chars at the

2020-03-18 07:01发布

Quick example:

public class Test {
    public static void main(String[] args) {
        String str = "   a b";
        String[] arr = str.split("\\s+");
        for (String s : arr)
            System.out.println(s);
    }
}

I want the array arr to contain 2 elements: "a" and "b", but in the result there are 3 elements: "" (empty string), "a" and "b". What should I do to get it right?

4条回答
看我几分像从前
2楼-- · 2020-03-18 07:27

Instead of trimming, you could just add an if to check if a string is empty or not.

查看更多
甜甜的少女心
3楼-- · 2020-03-18 07:28

The other way to trim it is to use look ahead and look behind to be sure that the whitespace is sandwiched between two non-white-space characters,... something like:

String[] arr = str.split("(?<=\\S)\\s+(?=\\S)");

The problem with this is that it doesn't trim the leading spaces, giving this result:

   a
b

but nor should it as String#split(...) is for splitting, not trimming.

查看更多
再贱就再见
4楼-- · 2020-03-18 07:51

Kind of a cheat, but replace:

String str = "   a b";

with

String[] arr = "   a b".trim().split("\\s+");
查看更多
Ridiculous、
5楼-- · 2020-03-18 07:52

The simple solution is to use trim() to remove leading (and trailing) whitespace before the split(...) call.

You can't do this with just split(...). The split regex is matching string separators; i.e. there will necessarily be a substring (possibly empty) before and after each matched separator.

You can deal with the case where the whitespace is at the end by using split(..., 0). This discards any trailing empty strings. However, there is no equivalent form of split for discarding leading empty strings.

查看更多
登录 后发表回答