How to split a String by space

前端 未结 15 1531
傲寒
傲寒 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 10:57

    if somehow you don't wanna use String split method then you can use StringTokenizer class in Java as..

        StringTokenizer tokens = new StringTokenizer("Hello I'm your String", " ");
        String[] splited = new String[tokens.countTokens()];
        int index = 0;
        while(tokens.hasMoreTokens()){
            splited[index] = tokens.nextToken();
            ++index;
        }
    

提交回复
热议问题