Java 检查字符串中特定单词的数量

Java 检查字符串中特定单词的数量,java,Java,如果我想比较一个词和另一个词的使用量,我该怎么做 它不是str.contains(“猫”)>str.contains(“狗”) 例如: if(str.contains("cat") == str.contains("dog")){ System.out.println("true"); } else system.out.print("false"); 如果猫和狗出现的次数相同,则假设打印结果为真。但是很明显,它没有,我需要做什么才能让它检查呢?String#contains()如

如果我想比较一个词和另一个词的使用量,我该怎么做

它不是
str.contains(“猫”)>str.contains(“狗”)

例如:

if(str.contains("cat") == str.contains("dog")){
    System.out.println("true");
  }
else
system.out.print("false");
如果猫和狗出现的次数相同,则假设打印结果为真。但是很明显,它没有,我需要做什么才能让它检查呢?

String#contains()
如果搜索到的字符串至少找到一次,就会返回true,这是出于性能原因。因此,
str.contains(“cat”)==str.contains(“dog”)
如果猫和狗都是独立于它们被发现的频率而被发现的,那么它就是真的

您可以使用2个正则表达式并检查匹配数:

int countWords(String input, String word ) {
  Pattern p = Pattern.compile( "\\b" + word + "\\b" );

  int count = 0;    
  Matcher m = p.matcher( input );
  while( m.find() ) {
    count++;
  }

  return count;
}
用法:

String str = "dog eats dog but cat eats hotdog";

System.out.println("dogs: " + countWords( str, "dog"));
System.out.println("cats: " + countWords( str, "cat"));
输出:

dogs: 2
cats: 1

你的问题有很多可能的解决方案。我不会向您展示完整的解决方案,但会尝试指导您。以下是一些:

  • 字符串根据空格,在结果数组上迭代,并在匹配要查找的字符串时递增计数器

  • 使用与您要查找的单词完全匹配的正则表达式,和类中有许多有用的方法,请仔细查看它们

  • 有无数的方法,你可以在一行得到结果


  • 你可以用下面的方法计算狗的数量

    index = -1;
    dogcount = 0;
    do {
     index = str.indexOf("dog",index+1);
     if(index > -1)
       dogcount++;
    }while(index == -1);
    

    类似地,获取cat count来计算另一个字符串中某个字符串的出现次数创建函数():

    拆分计数法:

    public class CountSubstring {
        public static int countSubstring(String subStr, String str){
            // the result of split() will contain one more element than the delimiter
            // the "-1" second argument makes it not discard trailing empty strings
            return str.split(Pattern.quote(subStr), -1).length - 1;
    }
    
    public static int countSubstring(String subStr, String str){
            return (str.length() - str.replace(subStr, "").length()) / subStr.length();
    }
    
    删除并计算差异”方法:

    public class CountSubstring {
        public static int countSubstring(String subStr, String str){
            // the result of split() will contain one more element than the delimiter
            // the "-1" second argument makes it not discard trailing empty strings
            return str.split(Pattern.quote(subStr), -1).length - 1;
    }
    
    public static int countSubstring(String subStr, String str){
            return (str.length() - str.replace(subStr, "").length()) / subStr.length();
    }
    
    那么你只需要比较一下:

    return countSubstring("dog", phrase) > countSubstring("cat", phrase);
    
    其他信息

    要比较字符串,请使用或,如果您不是指
    大写
    小写

     string1.equals(string2);
     string1.equalsIgnoreCase(string2);
    
    要在另一个字符串中查找字符串的出现次数,请使用

    要查看一个字符串是否包含另一个字符串,请使用


    Use Apache Commons StringUtils.CountMatches:-统计一个字符串在另一个字符串中出现的次数


    为了完全正确,您需要使用indexOf、substring。。。使用空格拆分的简单版本可能会有所帮助,但如果使用“dog”,会不会将“hotdog”算作“dog”出现?拆分
    str
    后,可能会重复或使用Java8的流工具。因此,例如,字符串“catxxxdogxxxdogxxxcat”将为真,因为cat和god在字符串中的次数相同。“catxxdogxx”也是如此,但如果有更多像“catxxcatxxdog”这样的东西,那就错了。