1

I need to check if a string (word, no whitespaces) has any letters of a given alphabet.

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

I want to compare efficiently the string with the alphabet. What I want is to check if String consists of the letters in Alphabet. For now I have my alphabet in an ArrayList and in a for loop I check if String contains the letter of the arraylist and if true then exit, else continue with the next letter. The example above for String A will return false because p and l are not part of the alphabet. But it will return true for B.

Can this be done more efficiently? Thank you for your help.

Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • 1
    Regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html – Joetjah May 22 '13 at 14:25
  • Just for clarification, for you example, do you expect `true` (because the text contains a `a` which is part of the alphabet) or `false` (because the text contains an `p` which is not part of the alphabet)? – Andreas Dolk May 22 '13 at 14:33
  • Good question. Let me update my question. – Alkis Kalogeris May 22 '13 at 14:35
  • This is a semi-dupe of [Check if String contains only letters](http://stackoverflow.com/questions/5238491/check-if-string-contains-only-letters). – einpoklum May 23 '13 at 11:32

2 Answers2

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]+"))
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I'm sorry for the mistake but could you check my question again? It was wrong. Now it explains what I want. (Already upvoted you though) – Alkis Kalogeris May 22 '13 at 14:40
  • Thank you for both of the solutions. When I check them I'll accept your answer. – Alkis Kalogeris May 22 '13 at 14:45
  • You could even use ranges `"[a-f]+"`. – Joop Eggen May 22 '13 at 14:50
  • No, that's not an option. The characters will be utf-8 material. – Alkis Kalogeris May 22 '13 at 14:52
  • @JoopEggen yes, however the example given was a poor example - I think it was supposed to be arbitrary letters. – Bohemian May 22 '13 at 14:52
  • Is regex faster than doing it in a couple of loops? – Averroes May 22 '13 at 14:57
  • @Averroes My guess is that regex would probably be faster, but even if not, when programming one should do the simplest thing that works. If there's a performance problem, discover that in testing to prove where the problem is and how big it is first. Only then should you attempt optimization to replace simple code with complicated code that's faster. – Bohemian May 22 '13 at 15:01
  • Yes, I agree but I think the question was a bit misleading then because the OP is asking for a "more efficiently" way to do it so I thought he was asking for a more time efficiently. I ran a few benchmarks and the regex was slower than the loops. – Averroes May 22 '13 at 15:08
0

check this this will give you an idea how to do this check this..http://www.tutorialspoint.com/java/lang/string_contains.htm