1

文字列 (単語、空白なし) に特定のアルファベットの文字が含まれているかどうかを確認する必要があります。

String A: apple
String B: bed
Alphabet: a b c d e f

文字列とアルファベットを効率よく比較したい。私が欲しいのは、文字列がアルファベットの文字で構成されているかどうかを確認することです。今のところ、ArrayList にアルファベットがあり、for ループで String に arraylist の文字が含まれているかどうかを確認し、true の場合は終了し、そうでない場合は次の文字に進みます。上記の文字列 A の例では、p と l はアルファベットの一部ではないため、false が返されます。しかし、B に対しては true が返されます。

これをより効率的に行うことはできますか? ご協力ありがとうございました。

4

2 に答える 2

1

Turn the "alphabet" into a regex then use String.matches(). Not sure what you're after exactly, but I'm pretty sure it's one of there two options:

To check that the word has at least one of the letters in the alphabet:

if (str.matches(".*[abcdef].*"))

To check if the word consists only of the letters of the alphabet:

if (str.matches("[abcdef]+"))
于 2013-05-22T14:35:21.087 に答える
0

これを確認すると、これを確認する方法がわかります.. http://www.tutorialspoint.com/java/lang/string_contains.htm

于 2013-05-22T14:38:13.040 に答える