使用Java编写,从键盘输入一个字符串和一个字符,从该字符串中删除给定的字符
时间: 2024-05-07 09:15:16 浏览: 101
```
import java.util.Scanner;
public class DeleteCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = scanner.nextLine();
System.out.print("请输入要删除的字符:");
char c = scanner.nextLine().charAt(0);
String result = deleteCharacter(str, c);
System.out.println("删除后的字符串为:" + result);
}
private static String deleteCharacter(String str, char c) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch != c) {
sb.append(ch);
}
}
return sb.toString();
}
}
```
阅读全文