题解 | #计算某字符出现次数#
计算某字符出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.nextLine().toLowerCase();
String checkChar = scanner.nextLine().toLowerCase();
//方法一:通过replaceAll匹配待检查的字符,返回剩余的字符串,用原长度减去剩余长度就是该字符出现的次数
System.out.print(str.length()-str.replaceAll(checkChar,"").length());
//方法二:将输入字符串转成List,遍历List的同时判断检查字符是否与list元素有重复的,有则记录count
// int count = 0;
// List<String> strList = Arrays.asList(str.split(""));
// for(String k : strList){
// if(checkChar.equals(k))
// count++;
// }
// System.out.print(count);
}
}
}