How to split a String by space

前端 未结 15 1550
傲寒
傲寒 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:06

    Here is a method to trim a String that has a "," or white space

    private String shorterName(String s){
            String[] sArr = s.split("\\,|\\s+");
            String output = sArr[0];
    
            return output;
        }
    
    0 讨论(0)
  • 2020-11-22 11:07

    Very Simple Example below:

    Hope it helps.

    String str = "Hello I'm your String";
    String[] splited = str.split(" ");
    var splited = str.split(" ");
    var splited1=splited[0]; //Hello
    var splited2=splited[1]; //I'm
    var splited3=splited[2]; //your
    var splited4=splited[3]; //String
    
    0 讨论(0)
  • 2020-11-22 11:08

    Try

    String[] splited = str.split("\\s");
    

    http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html

    0 讨论(0)
提交回复
热议问题