如何使用空格字符分隔字符串作为分隔符?

时间:2023-01-11 21:48:00

What regex pattern would need I to pass to the java.lang.String.split() method to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters?

我需要将什么regex模式传递给java.lang.String.split()方法,以便使用所有空格字符(' '、'\t'、'\n'等)作为分隔符将字符串分割成一组子字符串?

12 个解决方案

#1


888  

Something in the lines of

在行的东西

myString.split("\\s+");

This groups all white spaces as a delimiter.

这将所有空格作为分隔符进行分组。

So if I have the string:

如果我有弦

"Hello[space][tab]World"

“你好(空间)[tab]世界”

This should yield the strings "Hello" and "World" and omit the empty space between the [space] and the [tab].

这将产生字符串“Hello”和“World”,并省略[space]和[tab]之间的空白。

As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "\s", which means, you need to pass "\\s". It can get a bit confusing.

正如VonC所指出的,应该转义反斜杠,因为Java将首先尝试将字符串转义为一个特殊的字符,然后将其发送给要解析的字符。你想要的是字面上的“\s”,意思是你需要通过“\s”。可能会有点混乱。

The \\s is equivalent to [ \\t\\n\\x0B\\f\\r]

\s = [\t\ n\ x0B\ f\ r]

#2


79  

In most regex dialects there are a set of convenient character summaries you can use for this kind of thing - these are good ones to remember:

在大多数regex方言中,有一组方便的字符总结,可以用来做此类事情——这些都是值得记住的好东西:

\w - Matches any word character.

\w -匹配任何字字符。

\W - Matches any nonword character.

\W -匹配任何非文字字符。

\s - Matches any white-space character.

\s -匹配任何空白字符。

\S - Matches anything but white-space characters.

\S -匹配除空白字符以外的任何字符。

\d - Matches any digit.

\d -匹配任何数字。

\D - Matches anything except digits.

\D -除了数字外,其他都匹配。

A search for "Regex Cheatsheets" should reward you with a whole lot of useful summaries.

搜索“Regex Cheatsheets”,你会得到很多有用的总结。

#3


55  

To get this working in Javascript, I had to do the following:

为了在Javascript中工作,我必须做以下工作:

myString.split(/\s+/g)

#4


35  

"\\s+" should do the trick

“只\s+”就可以了

#5


9  

Also you may have a UniCode non-breaking space xA0...

此外,还可以使用UniCode不间断空间xA0……

String[] elements = s.split("[\\s\\xA0]+"); //include uniCode non-breaking

#6


8  

Apache Commons Lang has a method to split a string with whitespace characters as delimiters:

Apache Commons Lang有一种方法,可以用空格字符分隔字符串作为分隔符:

StringUtils.split("abc def")

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#split(java.lang.String)

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html分裂(以)

This might be easier to use than a regex pattern.

这可能比regex模式更容易使用。

#7


8  

String string = "Ram is going to school";
String[] arrayOfString = string.split("\\s+");

#8


2  

Since it is a regular expression, and i'm assuming u would also not want non-alphanumeric chars like commas, dots, etc that could be surrounded by blanks (e.g. "one , two" should give [one][two]), it should be:

因为它是一个正则表达式,我假设u也不希望非字母数字字符如逗号、圆点等被空格包围。“一,二”应该给予[1][2],应该是:

myString.split(/[\s\W]+/)

#9


1  

you can split a string by line break by using the following statement :

可以使用以下语句将字符串按行分隔:

 String textStr[] = yourString.split("\\r?\\n");

you can split a string by Whitespace by using the following statement :

可以使用以下语句按空格分隔字符串:

String textStr[] = yourString.split("\\s+");

#10


1  

String str = "Hello   World";
String res[] = str.split("\\s+");

#11


1  

I'm surprised that nobody has mentioned String.split() with no parameters. Isn't that what it's made for? as in:

我很惊讶没有人提到String.split(),没有参数。这不是它的目的吗?如:

"abc def ghi".split()

#12


-1  

Study this code.. good luck

研究这段代码. .祝你好运

    import java.util.*;
class Demo{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.print("Input String : ");
        String s1 = input.nextLine();   
        String[] tokens = s1.split("[\\s\\xA0]+");      
        System.out.println(tokens.length);      
        for(String s : tokens){
            System.out.println(s);

        } 
    }
}

#1


888  

Something in the lines of

在行的东西

myString.split("\\s+");

This groups all white spaces as a delimiter.

这将所有空格作为分隔符进行分组。

So if I have the string:

如果我有弦

"Hello[space][tab]World"

“你好(空间)[tab]世界”

This should yield the strings "Hello" and "World" and omit the empty space between the [space] and the [tab].

这将产生字符串“Hello”和“World”,并省略[space]和[tab]之间的空白。

As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "\s", which means, you need to pass "\\s". It can get a bit confusing.

正如VonC所指出的,应该转义反斜杠,因为Java将首先尝试将字符串转义为一个特殊的字符,然后将其发送给要解析的字符。你想要的是字面上的“\s”,意思是你需要通过“\s”。可能会有点混乱。

The \\s is equivalent to [ \\t\\n\\x0B\\f\\r]

\s = [\t\ n\ x0B\ f\ r]

#2


79  

In most regex dialects there are a set of convenient character summaries you can use for this kind of thing - these are good ones to remember:

在大多数regex方言中,有一组方便的字符总结,可以用来做此类事情——这些都是值得记住的好东西:

\w - Matches any word character.

\w -匹配任何字字符。

\W - Matches any nonword character.

\W -匹配任何非文字字符。

\s - Matches any white-space character.

\s -匹配任何空白字符。

\S - Matches anything but white-space characters.

\S -匹配除空白字符以外的任何字符。

\d - Matches any digit.

\d -匹配任何数字。

\D - Matches anything except digits.

\D -除了数字外,其他都匹配。

A search for "Regex Cheatsheets" should reward you with a whole lot of useful summaries.

搜索“Regex Cheatsheets”,你会得到很多有用的总结。

#3


55  

To get this working in Javascript, I had to do the following:

为了在Javascript中工作,我必须做以下工作:

myString.split(/\s+/g)

#4


35  

"\\s+" should do the trick

“只\s+”就可以了

#5


9  

Also you may have a UniCode non-breaking space xA0...

此外,还可以使用UniCode不间断空间xA0……

String[] elements = s.split("[\\s\\xA0]+"); //include uniCode non-breaking

#6


8  

Apache Commons Lang has a method to split a string with whitespace characters as delimiters:

Apache Commons Lang有一种方法,可以用空格字符分隔字符串作为分隔符:

StringUtils.split("abc def")

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#split(java.lang.String)

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html分裂(以)

This might be easier to use than a regex pattern.

这可能比regex模式更容易使用。

#7


8  

String string = "Ram is going to school";
String[] arrayOfString = string.split("\\s+");

#8


2  

Since it is a regular expression, and i'm assuming u would also not want non-alphanumeric chars like commas, dots, etc that could be surrounded by blanks (e.g. "one , two" should give [one][two]), it should be:

因为它是一个正则表达式,我假设u也不希望非字母数字字符如逗号、圆点等被空格包围。“一,二”应该给予[1][2],应该是:

myString.split(/[\s\W]+/)

#9


1  

you can split a string by line break by using the following statement :

可以使用以下语句将字符串按行分隔:

 String textStr[] = yourString.split("\\r?\\n");

you can split a string by Whitespace by using the following statement :

可以使用以下语句按空格分隔字符串:

String textStr[] = yourString.split("\\s+");

#10


1  

String str = "Hello   World";
String res[] = str.split("\\s+");

#11


1  

I'm surprised that nobody has mentioned String.split() with no parameters. Isn't that what it's made for? as in:

我很惊讶没有人提到String.split(),没有参数。这不是它的目的吗?如:

"abc def ghi".split()

#12


-1  

Study this code.. good luck

研究这段代码. .祝你好运

    import java.util.*;
class Demo{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.print("Input String : ");
        String s1 = input.nextLine();   
        String[] tokens = s1.split("[\\s\\xA0]+");      
        System.out.println(tokens.length);      
        for(String s : tokens){
            System.out.println(s);

        } 
    }
}