Converting a sentence string to a string array of words in Java

后端 未结 16 2460
余生分开走
余生分开走 2020-12-01 00:04

I need my Java program to take a string like:

\"This is a sample sentence.\"

and turn it into a string array like:

{\"this\         


        
16条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 00:41

    The easiest and best answer I can think of is to use the following method defined on the java string -

    String[] split(String regex)
    

    And just do "This is a sample sentence".split(" "). Because it takes a regex, you can do more complicated splits as well, which can include removing unwanted punctuation and other such characters.

提交回复
热议问题