如何将一个句子分成两部分

问题描述:

如何在JAVA中将一个句子分成两部分?如果有以下内容

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

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

如何返回我喜欢Java 我喜欢Python 因此单独忽略< =>

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

public void changeSentence(String line)
{
    String[] words = line.split(" ");

    for(int i = 0; i < words.length; i++)
    {
        if(!(words[i].equals("<=>")))
        {


        }
    }
}


可以使用下面给出的类String

It can be done using the method given below of class String

METHOD: (public String[] split(String regex, int limit)




  • 正则表达式:您要删除的字符串/字符以及拆分剩余文本

  • 限制:应返回多少字符串

public class TestSplit

{

    public static void main(String args[])
    {
        String str = new String("I Love Java <=> I Love Python");


        for (String retval: str.split("<=> ",2))
        {

                System.out.println(retval);
        }


    }
}

输出:


我爱Java

I Love Java

我喜欢Python






还有其他一些我知道的事实大约列在下面


There are some other facts I am aware about are listed below


  • 如果你不指定限制'保持空白'/'指定0'那么编译器将拆分字符串每次'< =>'被发现
    例如

public class TestSplit

{

    public static void main(String args[])

    {

        String str = new String("I Love Java <=> I Love Python <=> I Love *");
        for (String retval: str.split("<=> "))
        {
                System.out.println(retval);
        }


    }
}

输出:


我喜欢Java

I Love Java

我喜欢Python

我喜欢*

I Love *