Split ("+") and split ("") are different

I want to erase the vacuum in the String.

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

result

java
example.java



aaa




bbb

but I want a result that

Java
example.java
aaa
BBB

therefore I use split ("+"). The result is correct. but I don’t understand how the separation ("+") occurs.

+4
source share
8 answers

split() . "+" " ". , "+" " ".

+5
  • .
  • , , .

split()

.

, + .

string.split(" +"), one or more of the previous element, ("").

+3

\\s+. .

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

, \\s .

\\s+, .

+1

String.split() . "" , , "+" , .

regexs: http://www.regular-expressions.info/

+1

"+" , "+" . .

split - regex not string, . split

0

, .

" " "java", " ", "example.java", " ", "", " ", "", " ", "", " ", "", " ", "", " ", "", " ", "aaa", " ", "", " ", "", " ", "", " ", "", " ", "bbb", " " , "java", "example.java", "", "", "", "", "", "", "aaa", "", "", "", "", "bbb". , , " " , "".

" +" , . , "java", " ", "example.java", " ", "aaa", " ", "bbb", , "java", "example.java", "aaa", "bbb".

0

string.split("\\s+"), \\s - white-space (, , ...)

0

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

Regular expression " ":

  • Align the space character literally.

Regular expression (" +"):

  • Match one single space character (tab, line, carriage return, vertical tab, form feed) between one and unlimited time. (Greedy)

Short:

"" splits the array into one space.

/ \ s / splits an array into any type of space character

'+' Match between one and unlimited time

0
source

Source: https://habr.com/ru/post/1608260/


All Articles