题解 | #统计字符#
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = sc.nextLine(); //[^a-zA-Z]是去匹配目标字符串中非a—z也非A—Z的字符,将所有非字母字符替换为空 String str1 = str.replaceAll("[^a-zA-Z]",""); //将所有非空格的替换为空 String str2 = str.replaceAll("[^ ]",""); //所有非数字的替换为空 String str3 = str.replaceAll("[^0-9]",""); //[^a-zA-Z0-9]表示“找到一个非字母也非数字的字符”,将所有非字母非数字替换为空 String str4 = str.replaceAll("[a-zA-Z0-9 ]",""); System.out.println(str1.length()); System.out.println(str2.length()); System.out.println(str3.length()); System.out.println(str4.length()); } }