How to split a sentence into two parts

后端 未结 5 1303
半阙折子戏
半阙折子戏 2021-01-21 20:29

How to split a sentence into two parts in JAVA? If there is the following

String sentence = \"I love Java <=> I love Python\"

How can I

5条回答
  •  囚心锁ツ
    2021-01-21 21:07

    How can I return I love Java and I love Python thus separately ignoring <=>?

    First of all as you have said that you want your method to return separate words (Strings technically), for that you need change your return type from void to String[ ]

    Second, you are using String[] words = line.split(" "); this will split the String where spaces appear which would yield you array of Strings containing

    I
    love
    Java
    <=>
    I 
    love
    Python
    

    as different words stored as separate Strings in your words array.

    so what you should do is Strings[] words=line.split("<=>"); and return words

    Full code should be like this

    public String[] changeSentence(String line)
    {
        String[] words = line.split("<=>");
        return words;
    }
    

提交回复
热议问题