我建議你使用正則表達式,因爲它是非常簡單和強大的
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "STRING1|STRING2|STRING3";
final String string = "xxxSTRING1xxxSTRING2xxx";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
STDOUT:
Full match: STRING1
Full match: STRING2
DEMO在線IDE:here
嘗試一切;正確地對其進行基準測試,並選擇最適合您的測試。 –
你可能想看看[Aho-Corasick](https://en.m.wikipedia.org/wiki/Aho-Corasick_algorithm),這會更好地搜索所有選項。 –