0

For example: after execution, the output of the String "hello world yo" and "hello   world  yo" should be strictly the same.

what's more, the output should be a String[] in which:

String[0] == "hello"; String[1] == "world"; String[2] == "yo";

so that other method can deal with the effective words latter.

I was thinking about String.split(" "), but the blanks between the words are uncertain, and will then cause an exception..

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
Aaron7Sun
  • 89
  • 1
  • 1
  • 7

5 Answers5

3

You can use

  String.split("\\s+") // one or more whitespace.

Dont use == for string comaprision instead use String.equals()

Edit for question in comment

what's the notation called? what if there is one or more "_" or "\n" ?

As you can see String#split() API accepts regex as parameter. The \s is shorthand character class for whitespace, whereas + is used to repeats the previous item once or more.

Now if you want to split String on

  1. _ ie. underscore --> "this__is_test".split("[_]+");

  2. \n ie. newline --> "this__is_test\n new line".split("\\r?\\n");

Regex Tutorial

Smit
  • 4,685
  • 1
  • 24
  • 28
  • The OP is not doing any string comparisons. – Brandon Apr 25 '13 at 18:15
  • @Brandon I am not sure about that. OP puts `==` so I made notion that OP trying to compare strings. So to make sure I put that there to get expected results. – Smit Apr 25 '13 at 18:17
  • thanks dear... actually, I use "==" just to indicate the value of the String[]... Thank you for your kindly remind !! and for the \\S+ thing.. actually, I dont quite understand the meaning behind.. what's the notation called? what if there is one or more "_" or "\n" ? – Aaron7Sun Apr 25 '13 at 18:25
  • @JiahaoSun I updated the answer. I hope this will solve your issue. – Smit Apr 25 '13 at 18:43
1

You can split on "\\s+". That splits on one or more whitespace characters.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

String.split() takes a regexp, so you can simply do String.split(" +").

Thomas
  • 174,939
  • 50
  • 355
  • 478
0

I think the split function takes regex, but if it doesn't then the below works.

The regex in this might not be right, but it demonstrates the concept of what you're trying to do.

Pattern p = Pattern.compile("(.*?)  *(.*)");
Matcher m = p.matcher(s);
if (m.matches()) {
   String name = m.group(1);
   String value = m.groupo(2);
}

for (int i = 1; i<=m.groupCount(); i++) {
    System.out.println(m.group(i));
}
aeros
  • 314
  • 1
  • 3
  • 14
0

you can use Regular Expression to split the String.

String.split(Regular Expression);

for multiple whitespace, you can use Regular Expression: " \\s+ ", which 's' stand for space.

"==" operator used to judge whether left and right is equal. for String, they are Object actually, which means that they are regard as reference(like the pointer in C).

So if you want compare the content of two Strings, you can use method equals(String) of String. e.g. str1.equals(str2)

iLeoDo
  • 397
  • 3
  • 11