我们有一个字符串输入,以下组合有效(例如sunday
,*sunday*
,sun*day*
,*sun*day
,su*nda*y
)
如果它只包含一个星号,那么这是一个错误的输入。
因此,鉴于上述输入,我如何检查字符串是否包含多个星号。
答案 0 :(得分:9)
int asterisk1 = input.indexOf('*');
boolean hasTowAsterisks = asterisk1 != -1 && input.indexOf('*', asterisk1+1) != -1;
编辑:此解决方案假设要求“至少有两个星号”。
答案 1 :(得分:4)
您可以将String.matches
与正则表达式一起使用:
"^.*(?:\\*.*){2}$"
如果您想要完全两个星号:
"^[^*]*(?:\\*[^*]*){2}$"
虽然对于这个任务来说,迭代字符串并计算星号可能更简单。
答案 2 :(得分:3)
至少有两种方式:
正则表达式
String regex = ".*\\*.*\\*.*";
boolean valid = input.matches(regex);
环
int asterisks = 0;
for (int i = 0; i < input.length(); i ++) {
if (input.charAt(i) == '*') {
asterisks++;
}
}
答案 3 :(得分:3)
这是一个适用于任何文字字符串的非正则表达式:
public static boolean containsNoneOrTwo(String haystack, String needle) {
int index = haystack.indexOf(needle);
return (index == -1) ||
haystack.indexOf(needle, index+1) == haystack.lastIndexOf(needle);
}
基本上算法是:
containsNoneOrTwo(haystack, needle)
= haystack contains no needle OR
haystack's second needle is also its last
答案 4 :(得分:2)
boolean hasTwoAsteriks(String str) {
int i;
if((i = str.indexOf("*")) != -1) {
if ((i = str.indexOf("*", i+1)) != -1)
return true;
return false;
}
答案 5 :(得分:0)
为了完整性(尽管已经提供了几个好的答案,我喜欢Mark和Joachim的最佳答案),这里有两个基于String.split(regex)和String.split(regex, limit)的版本:
(编辑,错误修复:)
boolean containsAtLeastTwoAsterisks = ("_" + myString + "_").split("\\*", 3).length == 3;
boolean containsExactlyTwoAsterisks = ("_" + myString + "_").split("\\*").length == 3;
我根据我们的答案写了a little benchmark(我知道,基准测试并不重要,但它们很有趣,我知道可能是废话。)无论如何,以下是样本运行的结果:
*********************************************************************************
Testing strings with one or less asterisk
Processor: bhups
Finished. Duration: 40 ms, errors: 0
Processor: Bozho (loop version)
Finished. Duration: 33 ms, errors: 0
Processor: Bozho (regex version)
Finished. Duration: 806 ms, errors: 0
Processor: Joachim Sauer
Finished. Duration: 24 ms, errors: 0 <-- winner
Processor: Mark Byers
Finished. Duration: 1068 ms, errors: 0
Processor: seanizer
Finished. Duration: 408 ms, errors: 0
*********************************************************************************
Testing strings with exactly two asterisks
Processor: bhups
Finished. Duration: 14 ms, errors: 0 <-- winner
Processor: Bozho (loop version)
Finished. Duration: 21 ms, errors: 0
Processor: Bozho (regex version)
Finished. Duration: 693 ms, errors: 0
Processor: Joachim Sauer
Finished. Duration: 14 ms, errors: 0 <-- winner
Processor: Mark Byers
Finished. Duration: 491 ms, errors: 0
Processor: seanizer
Finished. Duration: 340 ms, errors: 0
*********************************************************************************
Testing strings with more than two asterisks (not all processors will be included)
Skipping processor bhups
Processor: Bozho (loop version)
Finished. Duration: 63 ms, errors: 0 <-- winner
Skipping processor Bozho (regex version)
Skipping processor Joachim Sauer
Processor: Mark Byers
Finished. Duration: 1555 ms, errors: 0
Processor: seanizer
Finished. Duration: 860 ms, errors: 0
似乎非正则表达式比正则表达式快得多。我猜这就是你所期望的。
编辑:修正了错误的赢家。抱歉,joachim