删除一个字符串中的指定字母,如:字符串 “aca”,删除其中的 a 字母。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.print("请输入要截取的字符:");
Scanner scan = new Scanner(System.in);
String str=scan.nextLine();
char[] s = str.toCharArray();
System.out.print("请输入字符串:");
Scanner scan1 = new Scanner(System.in);
String str1=scan1.nextLine();
char[] s2 = str1.toCharArray();
deleteCharacters(s2, s);
return;
}
public static void deleteCharacters(char[] str, char[] charSet) {
int[] hash = new int[256];
if (charSet == null || charSet.length == 0) {
System.out.print(str);
return;
}
for (int i = 0; i < 256; i++)
hash[i] = 0;
for (int i = 0; i < charSet.length; i++)
hash[charSet[i]] = 1;
int currentIndex = 0;
for (int i = 0; i < str.length; i++) {
if (hash[str[i]] == 0)
str[currentIndex++] = str[i];
}
for(int i=0;i<currentIndex;i++){
System.out.print(str[i]+" ");
}
return;
}
}