Split Strings in java by words

后端 未结 8 1589
迷失自我
迷失自我 2020-12-16 06:06

How can I split the following word in to an array

That\'s the code

into

array
0 That
1 s
2 the
3 code

I tried

相关标签:
8条回答
  • 2020-12-16 06:42

    split uses regex and in regex ' is not special character so you don't need to escape it with \. To represent whitespaces you can use \s (which in String needs to be written as "\\s"). Also to create set of characters you can use "OR" operator | like a|b|c|d, or just use character class [abcd] which means exactly the same as (a|b|c|d).

    To makes things simple you can use

    String[] strs = str.split("'| ");
    

    or

    String[] strs = str.split("'|\\s");//to include all whitespaces
    

    or

    String[] strs = str.split("['\\s]");//equivalent of "'|\\s"
    
    0 讨论(0)
  • 2020-12-16 06:48

    If you want to split on non alphabetic chars

    String str = "That's the code";
    String[] strs = str.split("\\P{Alpha}+");
    for (String sstr : strs) {
            System.out.println(sstr);
    }
    

    \P{Alpha} matches any non-alphabetic character and this is called POSIX character you can read more about it in this link It is very useful. + indicates that we should split on any continuous string of such characters.

    and the output will be

    That
    s
    the
    code
    
    0 讨论(0)
  • 2020-12-16 06:49

    The best solution I've found to split by words if your string contains accentuated letters is :

    String[] listeMots = phrase.split("\\P{L}+");
    

    For instance, if your String is

    String phrase = "Salut mon homme, comment ça va aujourd'hui? Ce sera Noël puis Pâques bientôt.";
    

    Then you will get the following words (enclosed within quotes and comma separated for clarity) :

    "Salut", "mon", "homme", "comment", "ça", "va", "aujourd", "hui", "Ce", 
    "sera", "Noël", "puis", "Pâques", "bientôt".
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-16 06:51

    You can split according to non-characters chars:

    String str = "That's the code";
    String[] splitted = str.split("[\\W]");
    

    For your input, output will be:

    That
    s
    the
    code
    
    0 讨论(0)
  • 2020-12-16 06:54

    To specifically split on white space and the apostrophe:

    public class Split {
        public static void main(String[] args) {
            String [] tokens = "That's the code".split("[\\s']");
            for(String s:tokens){
                System.out.println(s);
            }
        }
    }
    

    or to split on any non word character:

    public class Split {
        public static void main(String[] args) {
            String [] tokens = "That's the code".split("[\\W]");
            for(String s:tokens){
                System.out.println(s);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 06:54

    You should first replace the ' with " " (blank space), using str.replaceAll("'", " ") and then you can split the string on the blank space separator, using str.split(" ").You could alternatively use a regular expression to split on ' OR space.

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