Update: My bad. I misread this as case insensitive search.
Case sensitive is easy. Java does not yet support String
s in switch
statements. The easiest solution is:
if (string1.equals(string2)) {
...
} else if (string1.equals(string3)) {
...
}
Or as a loop:
String[] matches = new String[] {
"abcd",
"efgh",
"ijkl"
};
for (String match : matches) {
if (matches.equals(string)) {
...
}
}
Of course, this is linear (O(n)) and doesn't scale but is simple and is sufficient for the simplest of cases. A better solution is to use a hash-based lookup:
Set<String> matches = new HashSet<String>();
matches.add("abcd");
matches.add("efgh");
matches.add("ijkl");
if (matches.contains(string1)) {
...
}
This is near-linear (O(1)) lookup and will scale much better with a large number of String
s.