简体   繁体   English

我如何查找字符串中使用子字符串多少次?

[英]How do i find how many times a substring is used in a string?

public Object countOccurrences(String string)
{
    int e = 0;
    int i = 0;
    while ( i < sentence.length())
    {
        if(sentence.contains(string))
        {
            e++;
            i++;
            return e;
        }
        else
        {
            break;
        }
    }
    return e;
}

What is wrong with my code that won't allow me to pass the test? 无法通过测试的代码有什么问题? is there anyway I can use the substring method inside a loop to do this? 无论如何,我可以在循环内使用substring方法来做到这一点吗?

@Test
public void testCountOccurrences()
{
    assertEquals(1, sc1.countOccurrences("ence"));
    assertEquals(2, sc1.countOccurrences("en"));
    assertEquals(1, sc2.countOccurrences("that"));
    assertEquals(0, sc2.countOccurrences("This"));

}

What is wrong with my code that won't allow me to pass the test? 无法通过测试的代码有什么问题?

Before doing anything else, you should consider how you could have worked out what was wrong for your code to start with. 在做其他事情之前,您应该考虑如何弄清楚代码从何而来。 Did you debug through it? 您调试了吗? At what point did it behave differently to how you expected it to? 它在什么时候表现出与您预期的不同? If you haven't learned how to use a debugger yet, now is a great time to start. 如果您还没有学习如何使用调试器,那么现在是开始的好时机。

As for the next step, look at this code: 至于下一步,请看以下代码:

if(sentence.contains(string))
{
    e++;
    i++;
    return e;
}

That condition doesn't depend on i or e , just on sentence and string . 该条件不取决于ie ,而仅取决于sentencestring So as long as sentence is of length at least 1, you'll either return 1 or 0. Your code can never return more than 1. 所以只要sentence是长度至少为1的,你要么返回1或0。您的代码不能返回超过 1。

That's what's wrong with your code at the moment - as for how to fix it, I'd start looking at String.indexOf(String, int) . 目前,这就是您的代码的问题-至于如何解决它,我将开始研究String.indexOf(String, int) In other words, you want to find the first occurrence, then find the next occurrence, then the next occurrence, until you can't find any more. 换句话说,您想要找到第一个事件,然后找到一个事件,然后找到 一个事件,直到找不到更多。 (Use the return value to work out where to start looking on the next iteration, as well as checking that there was a match.) (使用返回值可以确定从哪里开始寻找下一个迭代,以及检查是否存在匹配项。)

A couple of situations to be careful of: 需要注意以下两种情况:

  • How many times does "abbbc" contain "bb"? “ abbbc”包含“ bb”多少次?
  • How many times does "abbbc" contain ""? “ abbbc”包含多少次“”?

I'd also urge a couple of other changes: 我还敦促进行其他一些更改:

  • Your method has a return type of Object - why? 您的方法具有Object的返回类型-为什么? Surely it's always going to return an integer, so a return type of int would be more appropriate 当然,它总是要返回一个整数,所以int的返回类型会更合适
  • This is a great candidate for parameterized testing. 这是进行参数化测试的理想选择。 Look into how you can effectively separate your single test into multiple test cases which can pass or fail independently, but without the source overhead of lots of test methods... (Hint: each test case should have the sentence, the text you're looking for, and the expected number of matches.) 研究如何有效地将单个测试分为多个测试案例,这些测试案例可以独立通过或失败,但又没有很多测试方法的源代码开销……(提示:每个测试案例都应包含以下句子:寻找,以及预期的匹配数。)
public Object countOccurrences(String string) {
    int e = 0;
    int i = 0;
    while (i <= (sentence.length() - string.length() + 1)) {
        if (sentence.substr(i, string.length() - 1).equals(string)) {
            e++;
        }


        i++;
    }
    return e;
}

I didn't got to try it myself but it should work. 我没有必要亲自尝试,但是应该可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串和子字符串以及主字符串中存在多少次子字符串 - String and substring and how many times substring present in main string 如何使用findWithinHorizo​​n查找一个字母,该字母与字符串中出现的次数相同? - How do I use findWithinHorizon to find a letter as many times as it appears in a string? 如何在字符串中手动查找 substring? (爪哇) - How do I manually find a substring in a string? (Java) 您如何在数组/字符串中找到特定值以及在数组/字符串中出现了多少次 - How do you find a specific value in an array/string and how many times it occurs in the array/string 是否可以找到字符串中使用了多少个字母? - Is it possible to find how many letters are used in a string? 如何查找字符串对象在Java中重复多少次? - How to find how many times does a string object repeat in java? 如何通过从文件中读取整数来找到整数出现的次数和最长的序列? - How do I find how many times an integer occurs and the longest sequence by reading it from a file? 查找在Java中重复一个字符串中的字符多少次 - find how many numberof times a character from a String is repeated in java 检查字符串数组是否包含所需的子字符串后,如何提取与子字符串和子字符串本身连续的字符 - how do I extract characters consecutive to the substring and the substring itself After checking if string array contains the required substring html页面上这个词使用了多少次 - How many times the word is used on the html page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM