So for input:
arrondissement d
I Should get output:
Arrondissement de Boulogne-sur-Mer
Arrondissement Den Bosch
So it should give back both results. So in below code I've capitalized every first character of the word but this isn't correct because some words do not start with an upper case.
public ArrayList getAllCitiesThatStartWithLetters(String letters) {
ArrayList filteredCities = new ArrayList<>();
if (mCities != null) {
for (City city : mCities) {
if (city.getName().startsWith(new capitalize(letters))) {
filteredCities.add(city);
}
}
}
return filteredCities;
}
public String capitalize(String capString){
StringBuffer capBuffer = new StringBuffer();
Matcher capMatcher = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(capString);
while (capMatcher.find()){
capMatcher.appendReplacement(capBuffer, capMatcher.group(1).toUpperCase() + capMatcher.group(2).toLowerCase());
}
return capMatcher.appendTail(capBuffer).toString();
}
Answer
String
has a very useful regionMatches
method with an ignoreCase
parameter, so you can check if a region of a string matches another string case insensitively.
String alpha = "My String Has Some Capitals";
String beta = "my string";
if (alpha.regionMatches(true, 0, beta, 0, beta.length())) {
System.out.println("It matches");
}
No comments:
Post a Comment