我想检查一个字符串是否包含某个子串 n 次。我知道我能做到:
Assertions.assertThat(myString).contains("xyz");
甚至
Assertions.assertThat(myString).containsOnlyOnce("xyz");
但是我怎么能保证这n次呢?
我试过类似的方法:
Assertions.assertThat(myString).areExactly(n, myString.contains("xyz"));
但很遗憾,这是不可编译的。
有什么想法吗?
最佳答案
您可能正在寻找 StringUtils.countMatches
Counts the number of occurrences of one String in another
关于java - 如何确保一个字符串恰好有 n 次子串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291420/