Java 如何统计字数

统计字符串中的字数

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

实例

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

亲自试一试

相关页面

教程:Java 字符串

参考手册:Java 字符串方法