简体   繁体   English

如何将句子分为两部分

[英]How to split a sentence into two parts

How to split a sentence into two parts in JAVA? 如何在JAVA中将句子分为两部分? If there is the following 如果有以下情况

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

How can I return I love Java and I love Python thus separately ignoring <=> ? 如何返回I love JavaI love Python从而分别忽略<=>

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

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


        }
    }
}

It can be done using the method given below of class String 可以使用下面的类String给出的方法来完成

METHOD: (public String[] split(String regex, int limit)
  • Regex: The String/Character you wish to remove & split remaining text 正则表达式:您希望删除和拆分剩余文本的字符串/字符
  • Limit: How many String that should be returned 限制:应返回多少个字符串
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);
        }


    }
}

Output: 输出:

I Love Java 我爱Java

I Love Python 我爱Python


There are some other facts I am aware about are listed below 下面列出了我知道的其他一些事实

  • If you won't specify limit by 'keeping it blank'/'specify 0' then the compiler will split string every time '<=>' is found eg 如果您不会通过“保持空白” /“指定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);
        }


    }
}

Output: 输出:

I Love Java 我爱Java

I Love Python 我爱Python

I Love Stackoverflow 我爱Stackoverflow

Why not do: 为什么不这样做:

String[] words = line.split("<=>");
for(String word : words){
        System.out.println(word); 
}

Output: 输出:

I love Java 我爱Java

I love Python 我爱Python

public String[] changeSentence(String line){ 公共字符串[] changeSentence(字符串行){
String[] substrings = line.split("<=>"); String []子字符串= line.split(“ <=>”);
return substrings; 返回子字符串;
} }

You can also split with StringTokenizer. 您也可以使用StringTokenizer进行拆分。

The code for splitting the strings based upon delimiter is below: 下面是根据定界符分割字符串的代码:

StringTokenizer stringTokenizer = new StringTokenizer(sentence,"<=>");
        while(stringTokenizer.hasMoreTokens()) {
            System.out.println(stringTokenizer.nextToken());
        }

I hope this helps 我希望这有帮助

Thanks 谢谢

How can I return I love Java and I love Python thus separately ignoring <=>? 如何返回我爱Java和我爱Python从而分别忽略<=>的方式?

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[ ] 首先,正如您已经说过的那样,您希望您的方法返回单独的单词(技术上来说是字符串),为此您需要将返回类型从void更改为String []

Second, you are using String[] words = line.split(" "); 其次,您正在使用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. words数组中存储为单独的Strings不同单词。

so what you should do is Strings[] words=line.split("<=>"); 因此,您应该做的是Strings[] words=line.split("<=>"); and return words 然后回话

Full code should be like this 完整的代码应该像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM