Java 删除字符串中的单词(重新投资)
所以基本上我需要帮助我需要帮助做的是从刺痛中去掉一个单词。我不知道如何使用数组,字符等只是为了那些谁提到我Java 删除字符串中的单词(重新投资),java,string,Java,String,所以基本上我需要帮助我需要帮助做的是从刺痛中去掉一个单词。我不知道如何使用数组,字符等只是为了那些谁提到我 Output Ex: Enter a sentence: I really like Jolly Ranchers. Enter a string: really I like Jolly Ranchers. 我只需要从句子中删除所有出现的字符串。提前谢谢你的帮助 (不寻找讲义,可能是伪代码或其他示例。)请尝试此选项 String[] s = originalString.spl
Output Ex:
Enter a sentence: I really like Jolly Ranchers.
Enter a string: really
I like Jolly Ranchers.
我只需要从句子中删除所有出现的字符串。提前谢谢你的帮助
(不寻找讲义,可能是伪代码或其他示例。)请尝试此选项
String[] s = originalString.split(" ");
String wordToRemove = StandardInput;
String finalString ="";
for(String word : s){
if(!word.equals(wordToRemove )){
System.out.Print(word);
finalString += word+" ";
}
}
请试试这个
String[] s = originalString.split(" ");
String wordToRemove = StandardInput;
String finalString ="";
for(String word : s){
if(!word.equals(wordToRemove )){
System.out.Print(word);
finalString += word+" ";
}
}
我相信字符串有一个replace方法,所以如果有多个方法,可以尝试str.replace(“真的”、“真的”)或replaceach。我建议查看Java API以了解更多函数。我相信字符串有一个替换方法,所以如果有多个函数,请尝试类似str.replace(“真的”、“替换”)或replaceach的方法。我建议查看Java API了解更多函数。您可以这样做:
public static String remove(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence :");
String sentence = sc .nextLine();
System.out.println("Enter a word :");
String word = sc .nextLine();
String res ="";
for(String words : sentence.split(" ")){
if(!words.equalsIgnoreCase(word)){ //or words!=word if you want to NOT ignore the case
res+=words+" ";
}
}
return res;
}
public static void main(String[] args) {
System.out.println(remove());
}
它查看每个单词,只保留那些与您输入的单词不相等的单词
或者用更短的方式选择更多:
public static String remove(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence :");
String sentence = sc .nextLine();
System.out.println("Enter a word :");
String word = sc .nextLine();
return sentence.replaceAll(word,"");
}
你可以走这条路:
public static String remove(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence :");
String sentence = sc .nextLine();
System.out.println("Enter a word :");
String word = sc .nextLine();
String res ="";
for(String words : sentence.split(" ")){
if(!words.equalsIgnoreCase(word)){ //or words!=word if you want to NOT ignore the case
res+=words+" ";
}
}
return res;
}
public static void main(String[] args) {
System.out.println(remove());
}
它查看每个单词,只保留那些与您输入的单词不相等的单词
或者用更短的方式选择更多:
public static String remove(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence :");
String sentence = sc .nextLine();
System.out.println("Enter a word :");
String word = sc .nextLine();
return sentence.replaceAll(word,"");
}
使用replaceAll函数,
此函数获取两个参数
newString=str1.replaceAll(regex,substr)代码>
newString=str1.replaceAll(regex,substr)代码>
package se.samples;
import java.util.Scanner;
public class WordRemover {
static final String welcomeMessage = "Enter a sentence: ",
secondMessage = "Enter a string: ",
resultMessage = "";
static String replace(String source, String that){
return source.replaceAll(that, "");
}
public static void main(String[] args) throws Exception {
System.out.print(welcomeMessage);
try(Scanner s = new Scanner(System.in)){
String input = s.nextLine();
System.out.print(secondMessage);
System.out.println(resultMessage + replace(input, s.nextLine()));
}
}
}
是这样的
package se.samples;
import java.util.Scanner;
public class WordRemover {
static final String welcomeMessage = "Enter a sentence: ",
secondMessage = "Enter a string: ",
resultMessage = "";
static String replace(String source, String that){
return source.replaceAll(that, "");
}
public static void main(String[] args) throws Exception {
System.out.print(welcomeMessage);
try(Scanner s = new Scanner(System.in)){
String input = s.nextLine();
System.out.print(secondMessage);
System.out.println(resultMessage + replace(input, s.nextLine()));
}
}
}
查看使用字符串类
replace
method查看regex
和String.replaceAll()
…查看使用字符串类replace
method查看regex
和String.replaceAll()
…我本可以否决这一点,但我想我会让大家知道的,replaceach()方法是一个Apache Commons StringUtils方法,应该在这个答案中指出。String.replaceAll()将在不需要Apache的情况下完成此任务,当然,除非您已经在使用它。;)啊,是的,谢谢你花时间帮助别人打败新来的孩子。你真奇怪。我本来可以否决这个,但我想我应该让大家知道,replaceach()方法是一个Apache Commons StringUtils方法,应该在这个答案中指出。String.replaceAll()将在不需要Apache的情况下完成此任务,当然,除非您已经在使用它。;)啊,是的,谢谢你花时间帮助别人打败新来的孩子。你真奇怪。在发布任何代码之前,请测试它,以确保它实际工作,并且不会产生任何异常。这只会让那些想要利用您的通用代码概念的Java语言新手感到困惑和沮丧,您的通用代码概念很好,可以应用于许多不同的编码应用程序(我敢肯定)。对代码的简要解释或代码中的注释也很有帮助。我没有否决这篇文章,因为我相信你会纠正它。在发布任何代码之前,请测试它,以确保它实际工作,不会产生任何异常。这只会让那些想要利用您的通用代码概念的Java语言新手感到困惑和沮丧,您的通用代码概念很好,可以应用于许多不同的编码应用程序(我敢肯定)。对代码的简要解释或代码中的注释也很有帮助。我没有否决这篇文章,因为我相信你会纠正它。