这篇文章主要介绍了Java统计英文句子中出现次数最多的单词并计算出现次数的方法,涉及java针对英文句子的字符串遍历、转换、正则替换、计算、集合基本使用方法等相关操作技巧,需要的朋友可以参考下。 JAVA实现完整代码如下: import java.util.*; public class CountWord { public static void main(String[] args) { String str = ""; str = "If a person wants you alive and dead, it is not love, but possession. A person who really likes you and loves you will not try his best to get you, cheat you with lies, please you with rhetoric, but help you with his heart and plan for the future."; String result = countWord(str); System.out.println(result); }
//根据传入英文句子,统计出现最多次数的单词并返回 private static String countWord(String str) { //将逗号、句号、问号用空格替换 String result = ""; str = str.replace(","," ").replace("."," ").replace("'"," ").replace("?"," ");//将'号用空格替换 System.out.println(