split(“ +”) and split(“ ”) are different

佐手、 提交于 2021-02-16 18:07:08

问题


I want to erase the vacuum in the String.

String input = "java example.java       aaa     bbb";
String[] temp = input.trim().split(" ");

that result is

java
example.java




aaa




bbb

but i want result that

java
example.java
aaa
bbb

so, i use the split(" +"). The result is right. but i don't understand, how doing the split(" +").


回答1:


split() takes a regex as it's argument. "+" in regex means "one or more of the previous element". So splitting on " +" will split on "one or more spaces".




回答2:


  • In first case it's a normal string split.
  • Where as in second case, you took help of regular expression.

From docs of split() method

Splits this string around matches of the given regular expression.

A simple space don't have any expression where as + have.

string.split(" +") to set the split delimiter to one or more of the previous element, in your case be any number of white space characters (" ").




回答3:


Best way to provide the regex for whitespace is using \\s+. Which is one or more space.

String input = "java example.java       aaa     bbb";
String[] temp = input.trim().split("\\s+");

If you are expecting only single space then use \\s for single white space.

For more than one white space always use \\s+, that is what happening in your case.




回答4:


The parameter to String.split() is a regex or regular expression. Passing " " means it will match on exactly one space, passing " +" means it will match on one or more spaces.

There's a good tutorial on regexs here: http://www.regular-expressions.info/




回答5:


"+" in regex means one or more from previous , so " +" means one or more spaces. Refer regex syntax

And the argument in split method is regex not string as most believe. Refer split




回答6:


The argument is a regular expression for the text between items that gets thrown away.

With " ", the string is cut up as "java", " ", "example.java", " ", "", " ", "", " ", "", " ", "", " ", "", " ", "", " ", "aaa", " ", "", " ", "", " ", "", " ", "", " ", "bbb", and then the " " are thrown away, leaving "java", "example.java", "", "", "", "", "", "", "aaa", "", "", "", "", "bbb". Where there are many spaces in a row, each space matches the " " pattern separately, and there is an empty string "" in between each pair of spaces.

With " +", it means that any group of one or more spaces is a single match. So the string is cut up as "java", " ", "example.java", " ", "aaa", " ", "bbb", and the space groups are thrown away, leaving "java", "example.java", "aaa", "bbb".




回答7:


I recommend using string.split("\\s+"), \\s is any white-space character (space, tab, enter...)




回答8:


The difference between .split(" ") and .split(+) is:

The regex " ":

  • Match the space character literally.

The regex (" +"):

  • Match a single whitespacecharacter (tab, line feed, carriage return, vertical tab, form feed) between one and unlimmited times. (greedy)

Short:

" " splits the array at one single space character.

/\s/ splits the array at every kind of whitespace character

'+' Matches between one and unlimitted times



来源:https://stackoverflow.com/questions/32687347/split-and-split-are-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!