java 如何检查字符串是否包含两个星号字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3350804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a string contains two asterisk characters?
提问by Achaius
We have a string input and the following combinations are valid (e.g. sunday, *sunday*, sun*day*, *sun*day, su*nda*y)
If it contains only a single asterisk, then it is a bad input.
我们有一个字符串输入并且以下组合是有效的(例如sunday, *sunday*, sun*day*, *sun*day, su*nda*y) 如果它只包含一个星号,那么它是一个错误的输入。
So given the above input, how do I check to see if the string contains multiple asterisks.
因此,鉴于上述输入,我该如何检查字符串是否包含多个星号。
采纳答案by Mark Byers
You could use String.matcheswith a regular expression:
您可以使用String.matches正则表达式:
"^.*(?:\*.*){2}$"
If you want exactlytwo asterisks:
如果你想正好两个星号:
"^[^*]*(?:\*[^*]*){2}$"
Though for this task it might be simpler just to iterate over the string and count the asterisks.
尽管对于此任务,只需遍历字符串并计算星号可能会更简单。
回答by Joachim Sauer
int asterisk1 = input.indexOf('*');
boolean hasTowAsterisks = asterisk1 != -1 && input.indexOf('*', asterisk1+1) != -1;
Edit: this solution assumed that the requirement was "has at least two asterisks".
编辑:此解决方案假定要求是“至少有两个星号”。
回答by polygenelubricants
Here's a non-regex alternative that works with any literal string:
这是一个适用于任何文字字符串的非正则表达式替代方案:
public static boolean containsNoneOrTwo(String haystack, String needle) {
int index = haystack.indexOf(needle);
return (index == -1) ||
haystack.indexOf(needle, index+1) == haystack.lastIndexOf(needle);
}
Essentially the algorithm is:
本质上算法是:
containsNoneOrTwo(haystack, needle)
= haystack contains no needle OR
haystack's second needle is also its last
回答by Bozho
At least two ways:
至少有两种方式:
regex
String regex = ".*\*.*\*.*"; boolean valid = input.matches(regex);loop
int asterisks = 0; for (int i = 0; i < input.length(); i ++) { if (input.charAt(i) == '*') { asterisks++; } }
正则表达式
String regex = ".*\*.*\*.*"; boolean valid = input.matches(regex);环形
int asterisks = 0; for (int i = 0; i < input.length(); i ++) { if (input.charAt(i) == '*') { asterisks++; } }
回答by bhups
boolean hasTwoAsteriks(String str) {
int i;
if((i = str.indexOf("*")) != -1) {
if ((i = str.indexOf("*", i+1)) != -1)
return true;
return false;
}
回答by Sean Patrick Floyd
For completeness (although several good answers have been provided, I like Mark's and Joachim's best), here are two versions based on String.split(regex)and String.split(regex, limit):
为了完整性(虽然已经提供了几个很好的答案,我最喜欢 Mark 和 Joachim 的答案),这里有两个基于String.split(regex)和String.split(regex, limit) 的版本:
(Edit, bug fix:)
(编辑,错误修复:)
boolean containsAtLeastTwoAsterisks = ("_" + myString + "_").split("\*", 3).length == 3;
boolean containsExactlyTwoAsterisks = ("_" + myString + "_").split("\*").length == 3;
I wrote a little benchmarkbased on our answers (I know, benchmarks don't mean much, but they are fun, and mine is probably crap, I know.) Anyway, here are the results for a sample run:
我根据我们的回答写了一个小基准(我知道,基准没有多大意义,但它们很有趣,而我的可能是废话,我知道。)无论如何,以下是示例运行的结果:
*********************************************************************************
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
Seems like non-regex is a lot faster than regex. That's what you'ld expect, I guess.
似乎非正则表达式比正则表达式快得多。这就是你所期望的,我猜。
EDIT: fixed wrong winner. sorry, joachim
编辑:修正错误的获胜者。对不起,约阿希姆

