Split Strings in java by words

后端 未结 8 1611
迷失自我
迷失自我 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: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!

提交回复
热议问题