Java 如何统计字数

统计字符串中的字数

您可以通过下例轻松地统计字符串中的字数:

实例

String words = "One Two Three Four";
int countWords = words.split("\\s").length;
System.out.println(countWords);

亲自试一试