Split a String in Java – 在Java中分割一个字符串

最后修改: 2017年 5月 15日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

Splitting Strings is a very frequent operation; this quick tutorial is focused on some of the API we can use to do this simply in Java.

分割字符串是一个非常频繁的操作;本快速教程主要介绍我们可以在Java中简单地使用一些API来完成这个操作。

2. String.split()

2.String.split()

Let’s start with the core library – the String class itself offers a split() method – which is very convenient and sufficient for most scenarios. It simply splits the given String based on the delimiter, returning an array of Strings.

让我们从核心库开始– String类本身提供了一个split()方法–它非常方便,足以应付大多数情况。它简单地根据分隔符分割给定的String,返回一个Strings数组。

Let us look at some examples. We’ll start with splitting by a comma:

让我们看看一些例子。我们将从用逗号分割开始。

String[] splitted = "peter,james,thomas".split(",");

Let’s split by a whitespace:

让我们用一个空格来分割。

String[] splitted = "car jeep scooter".split(" ");

Let’s also split by a dot:

让我们也用一个点来分割。

String[] splitted = "192.168.1.178".split("\\.")

Let’s now split by multiple characters – a comma, space, and hyphen through regex:

现在让我们通过regex来分割多个字符–逗号、空格和连字符。

String[] splitted = "b a, e, l.d u, n g".split("\\s+|,\\s*|\\.\\s*"));

3. StringUtils.split()

3.StringUtils.split()

Apache’s common lang package provides a StringUtils class – which contains a null-safe split() method, that splits using whitespace as the default delimiter:

Apache的通用语言包提供了一个StringUtils类–它包含一个空安全的split()方法,使用空白作为默认分隔符进行分割。

String[] splitted = StringUtils.split("car jeep scooter");

Furthermore, it ignores extra spaces:

此外,它忽略了多余的空格。

String[] splitted = StringUtils.split("car   jeep  scooter");

4. Splitter.split()

4.Splitter.split()

Finally, there’s a nice Splitter fluent API in Guava as well:

最后,在Guava中也有一个不错的Splitter流畅的API。

List<String> resultList = Splitter.on(',')
  .trimResults()
  .omitEmptyStrings()
  .splitToList("car,jeep,, scooter");

5. Split and Trim

5.分割和修剪

Sometimes a given String contains some leading, trailing, or extra spaces around the delimiter. Let’s see how we can handle splitting the input and trimming the results in one go.

有时,一个给定的String包含一些前导、尾部或分隔符周围的额外空格。让我们看看如何一次性处理分割输入和修剪结果的问题。

Let’s say we have this as an input:

比方说,我们有这样一个输入。

String input = " car , jeep, scooter ";

To remove extra spaces before and/or after the delimiter, we can perform split and trim using regex:

为了删除分隔符之前和/或之后的额外空格,我们可以使用regex进行分割和修剪。

String[] splitted = input.trim().split("\\s*,\\s*");

Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter.

在这里,trim()方法删除了输入字符串中的前导和尾部空格,而regex本身则处理了定界符周围的额外空格。

We can achieve the same result by using Java 8 Stream features:

我们可以通过使用Java 8的Stream功能实现同样的结果。

String[] splitted = Arrays.stream(input.split(","))
  .map(String::trim)
  .toArray(String[]::new);

6. Conclusion

6.结论

String.split() is generally enough. However, for more complex cases we can utilize Apache’s commons-lang based StringUtils class, or the clean and flexible Guava APIs.

String.split()通常就足够了。然而,对于更复杂的情况,我们可以利用Apache的基于commons-lang的StringUtils类,或者干净而灵活的Guava APIs。

And, as always, the code for the article is available over on GitHub.

而且,像往常一样,文章的代码可以在GitHub上找到