将字符串分割成字符串数组

时间:2023-01-16 21:31:17

I'm trying to find a way to split a String into an array of String(s), and I need to split it whenever a white spice is encountered, example

我正在尝试找到一种方法,将一个字符串分割成一个字符串数组(s),我需要在遇到一个白色的spice时将它拆分。

"hi i'm paul"

“嗨,我是保罗”

into"

为“

"hi" "i'm" "paul"

“嗨”“我”“保罗”

How do you represent white spaces in split() method using RegularExpression?

如何使用正则表达式在split()方法中表示空格?

2 个解决方案

#1


47  

You need a regular expression like "\\s+", which means: split whenever at least one whitespace is encountered. The full Java code is:

您需要一个像“\s+”这样的正则表达式,这意味着:只要遇到至少一个空格,就进行分割。完整的Java代码是:

try {
    String[] splitArray = input.split("\\s+");
} catch (PatternSyntaxException ex) {
    // 
}

#2


7  

String[] result = "hi i'm paul".split("\\s+"); to split across one or more cases.

String[] result = "hi i'm paul".split("\ s+");把一个或多个案例分开。

Or you could take a look at Apache Common StringUtils. It has StringUtils.split(String str) method that splits string using white space as delimiter. It also has other useful utility methods

或者你可以看看Apache通用的StringUtils。stringutil的。split(String str)方法,它使用空格作为分隔符分隔字符串。它还有其他有用的实用方法

#1


47  

You need a regular expression like "\\s+", which means: split whenever at least one whitespace is encountered. The full Java code is:

您需要一个像“\s+”这样的正则表达式,这意味着:只要遇到至少一个空格,就进行分割。完整的Java代码是:

try {
    String[] splitArray = input.split("\\s+");
} catch (PatternSyntaxException ex) {
    // 
}

#2


7  

String[] result = "hi i'm paul".split("\\s+"); to split across one or more cases.

String[] result = "hi i'm paul".split("\ s+");把一个或多个案例分开。

Or you could take a look at Apache Common StringUtils. It has StringUtils.split(String str) method that splits string using white space as delimiter. It also has other useful utility methods

或者你可以看看Apache通用的StringUtils。stringutil的。split(String str)方法,它使用空格作为分隔符分隔字符串。它还有其他有用的实用方法