Java编程:从输入的字符串中删去输入的所有子字符串
Java编程:从输入的字符串中删去输入的所有子字符串,例如:输入字符串"thisastring",输入子字符串“is”,则删除后的字符串“thastring”...
Java编程:从输入的字符串中删去输入的所有子字符串,例如:输入字符串"this a string",输入子字符串“is”,则删除后的字符串“th a string”
展开
3个回答
展开全部
import java.util.Scanner;
public class Du {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please input the source string: ");
String source = scanner.nextLine();
System.out.print("Please input substring: ");
String subStr = scanner.next();
String newStr = source.replaceAll(subStr, "");
System.out.println("After removing the " + subStr + ", the source string is: " + newStr);
}
}
----------------testing
Please input the source string: this a string
Please input substring: is
After removing the is, the source string is: th a string
public class Du {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please input the source string: ");
String source = scanner.nextLine();
System.out.print("Please input substring: ");
String subStr = scanner.next();
String newStr = source.replaceAll(subStr, "");
System.out.println("After removing the " + subStr + ", the source string is: " + newStr);
}
}
----------------testing
Please input the source string: this a string
Please input substring: is
After removing the is, the source string is: th a string
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Admin {
public static void main(String... args) {
String str = "this a string";
str = test(str, "is");
System.out.println(str);
}
private static String test(String str, String string) {
return str.replaceAll(string, "");
}
}
public static void main(String... args) {
String str = "this a string";
str = test(str, "is");
System.out.println(str);
}
private static String test(String str, String string) {
return str.replaceAll(string, "");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询