public class TestStr {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入字符串:");
String str = scan.next();
System.out.println("输入次数:");
int times = scan.nextInt();
char chr = getChar(str, times);
if(chr != ' ') {
System.out.println(chr);
}else {
System.out.println("未找到满足要求的值");
}
}
public static char getChar(String str, int times) {
for (int i = 0; i < str.length(); i++) {
String param = "[^(" + str.charAt(i) +")]"; //正则表达式
String s = str.replaceAll(param, "").trim(); //把不满足正则表达式的替换成空字符
if(s.length() >= times) { //判断匹配后的字符长度是否满足
return str.charAt(i);
}
}
return ' ';
}
}