How to ensure a string has a substring exactly n times?
Asked Answered
D

5

17

I want to check whether a String contains a certain substring n times. I know I can do:

Assertions.assertThat(myString).contains("xyz");

Or even Assertions.assertThat(myString).containsOnlyOnce("xyz");

But how can I ensure this for n times?

I tried something like:

Assertions.assertThat(myString).areExactly(n, myString.contains("xyz")); but sadly this is not compilable.

Any ideas?

Dielectric answered 15/12, 2015 at 14:13 Comment(3)
This is tricky btw. What about assertThat("aaa").areExactly(2, "aa")? Is this true?Loux
@Loux No, that does not even compileThorwald
@JoelCostigliola I know it doesn't compile. The point was not about making that compile but what it should return. Is "aa" present 2 or 1 time in "aaa"? That's why I said it was tricky.Loux
D
17

You are probably looking for StringUtils.countMatches

Counts the number of occurrences of one String in another

Dubonnet answered 15/12, 2015 at 14:16 Comment(2)
And another library... sighs No aspectj/junit solution alone?Dielectric
@Kayaman I think he was actually referring to assertj library (the library he used to test assertions), not aspectj. :-) And from this point of view (and looking at how many different assertions assertj already implements), it would be convinient to have containsTimes(int n, String expected) in addition to containsOnlyOnce(String expected).Reticent
B
6

You can have the regex like this which test String "myString" has SubString "xyz" n times.

Assertions.assertThat(myString).containsPattern("(xyz.*?){n}");

You can replace n with the count of substring you want to test.

Brannon answered 25/3, 2020 at 8:46 Comment(0)
T
0

The AssertJ way would be to use a Condition but it requires a little bit of work to implement it so I would use StringUtils.countMatches as Rahul Tripathi suggested.

Thorwald answered 15/12, 2015 at 20:37 Comment(0)
E
0

Hamcrest has matchers for regex. You could do

assertThat(actual, matchesPattern("(mystring.*?){2}))

Which will test that mystring is shown up exactly twice

http://hamcrest.org/JavaHamcrest/javadoc/2.0.0.0/org/hamcrest/text/MatchesPattern.html

and yes, another library, but for assertions this should be the only one you need. :-)

Emphysema answered 11/9, 2019 at 16:40 Comment(0)
H
-1

You could do something like:

        String testString = "XXwwYYwwLOK";
        int count = 0;
        while (testString.contains("ww")) {
            count++;
            int index = testString.indexOf("ww");
            if(index + 1 == testString.length()){
                break;
            }
            testString = testString.substring(index + 1);
        }

I didn't test this code but it should work.

Henri answered 15/12, 2015 at 14:30 Comment(1)
Good for you for contributing, but in this case this doesn't answer the question as the OP is asking about doing this within a unit test, ideally with a matcher. Besides that, it's best not to post code you didn't run.Emphysema

© 2022 - 2024 — McMap. All rights reserved.