How to split a String by space

前端 未结 15 1846
傲寒
傲寒 2020-11-22 10:31

I need to split my String by spaces. For this I tried:

str = \"Hello I\'m your String\";
String[] splited = str.split(\" \");

But it doesn\

15条回答
  •  无人共我
    2020-11-22 11:00

    What you have should work. If, however, the spaces provided are defaulting to... something else? You can use the whitespace regex:

    str = "Hello I'm your String";
    String[] splited = str.split("\\s+");
    

    This will cause any number of consecutive spaces to split your string into tokens.

    As a side note, I'm not sure "splited" is a word :) I believe the state of being the victim of a split is also "split". It's one of those tricky grammar things :-) Not trying to be picky, just figured I'd pass it on!

提交回复
热议问题