[英]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 Java
和I 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)
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 下面列出了我知道的其他一些事实
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.