Java 计算另一个字符串中出现的字符串数

Java 计算另一个字符串中出现的字符串数,java,string,Java,String,这是我的任务。我不允许使用if语句 编写一个通过命令行接收两个字符串的程序NumStrings.java,如下所示: 输入第二个字符串作为子字符串出现在 首先 我的坏代码: public class Test { public static void main(String[] args) { String a = "HelloHelloHelloHello"; String b = "Hello"; int times = 0;

这是我的任务。我不允许使用if语句

编写一个通过命令行接收两个字符串的程序NumStrings.java,如下所示: 输入第二个字符串作为子字符串出现在 首先

我的坏代码:

public class Test {


    public static void main(String[] args) {
        String a = "HelloHelloHelloHello";
        String b = "Hello";
        int times = 0;
        for(int i=0; i <= a.length()-5; i++){
            for (int z=4; z<=(a.length()-1) && a.compareTo(b)==0; z++){
                times = times +1;
            }
        }
        System.out.print(times);
    }
}

下面是正确的方法,请在此处使用子字符串文档:


这样看:您不必计算在第一个字符串中找到第二个字符串的频率,因为您必须始终检查是否找到它。因此,为了避免各种条件或if语句,请考虑使用FixStord.StudiTaskStudio.< 每次基本字符串找到子字符串时,拆分基本字符串后,splitsomeString将返回剩余子字符串的数组:

String first = "bababa";
String second = "a";
String[] substrings = first.split(second);
现在子字符串将如下所示:[b,b,b],因为每个a都已删除,其余的放在单独的字符串中

接下来,您必须检查数组的大小,您将看到第一个字符串被拆分的频率

int count = substrings.length;   // 3
然而,这并不是结束,因为我们仍然有以下情况:

 String first = "bababaa";
使用上述解决方案,您将得到大小为3的数组:[b,b,b]。最后一次出现的只会被删除,不会留下任何子字符串,甚至不会留下空子字符串

因此,您可以利用另一个稍微不同的拆分:

其中limit是方法尝试查找的最大出现次数。那么,你多久能在第一个字符串中找到第二个字符串?第一个字符串包含的字母数:int limit=first.length

你能看到发生了什么吗?在结尾处有两个空字符串,其中有两个a。对于在第二个字符串出现之前或之后找到的所有内容,都会得到一个子字符串数组

当然,当你拆分字符串ba时,你会得到[b,]so2个子字符串。但你不关心B,只是中间的逗号,对于每一个A,

编辑 谢谢@saka129!因此,当first=aaa和second=aa时,split方法仍然会遗漏一些内容,因为它只计算1次而不是2次。 为了纠正这一点,我想循环遍历整个第一个字符串,只检查第一次出现的情况,然后删除第一个字母并继续,因为OP已经接受了另一个答案,我只需发布我的代码:


你的代码有很多问题。而且,你没有完成任务。如果这必须接收命令行参数,那么你必须使用args数组。我知道,但我想用一个更简单的代码测试它。当a=ZZZ和b=ZZ时,它应该返回1或2?它应该打印2。我有另一个问题,我需要编辑一个字符串,但我们仍然没有在课堂上了解到这一点,但我在5小时内就要交给它了$@MohammadAlBaba你应该问一个新问题。这取决于你需要修改它的方式和时间以及结果应该存储在哪里。@MohammadAlBaba但是,我相信如果你搜索已经被问过的问题,你会找到已经问过这样一个问题的人。它返回1而不是2表示first=ZZZ和second=ZZ.oh!我真不敢相信我错过了。@saka1029:我知道现在已经太晚了,但是你怎么看?你能找到另一个问题吗?
int count = substrings.length;   // 3
 String first = "bababaa";
  first.split(second, limit);   
 first.split(second, first.length);  // will produce [b, b, b, , ]
 first.split(second, first.length).length -1;   // that's how many commas there are, and thats how many second strings there are
  String first = "ZZZ";
  String second = "ZZ";
  int counter = 0;           // counts the number of occurrences
  int n = first.length();    // needs to be fixed, because we'll change the length of the first string in the loop
  for(int i = 0; i < n; i++){  // check the first string letter by letter
      String[] split = first.split(second, 2);  // we want one substring and the rest (2 in total) like: ['', 'Z'] when we cut off the first 'ZZ'
      counter += split.length - 1;  // now add the number of occurrences (which will be either 0 or 1 in our case)
      first = first.substring(1);   // cut off the first letter of the first string and continue
   }
   System.out.println("counter = " + counter);  // now we should get 3 for 'ZZZ'