java实验步骤5 - 删除字符串中的指定字符:a 从键盘输入一个字符串和一个字符。b 使用字符串的replace方法将指定字符替换为空字符串从而删除该字符。c 输出删除指定字符后的字符串。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = input.nextLine();
System.out.print("请输入要删除的字符:");
char c = input.nextLine().charAt(0);
String result = str.replace(String.valueOf(c), "");
System.out.println("删除指定字符后的字符串为:" + result);
}
}
原文地址: http://www.cveoy.top/t/topic/hpHD 著作权归作者所有。请勿转载和采集!