如何将一个句子分成两部分 [英] How to split a sentence into two parts

查看:171
本文介绍了如何将一个句子分成两部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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 Stackoverflow");
            for (String retval: str.split("<=> "))
            {
                    System.out.println(retval);
            }
    
    
        }
    }
    

    输出:


    我喜欢Java

    I Love Java

    我喜欢Python

    我喜欢Stackoverflow

    I Love Stackoverflow

    这篇关于如何将一个句子分成两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆