How to split a String by space

前端 未结 15 1571
傲寒
傲寒 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条回答
  •  -上瘾入骨i
    2020-11-22 10:57

    Since it's been a while since these answers were posted, here's another more current way to do what's asked:

    List output = new ArrayList<>();
    try (Scanner sc = new Scanner(inputString)) {
        while (sc.hasNext()) output.add(sc.next());
    }
    

    Now you have a list of strings (which is arguably better than an array); if you do need an array, you can do output.toArray(new String[0]);

提交回复
热议问题