Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(" ");

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(" +").

like image 975
seung-jun kim Avatar asked Dec 24 '22 14:12

seung-jun kim


1 Answers

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".

like image 159
Patrick Stephen Avatar answered Jan 06 '23 22:01

Patrick Stephen