-1

I realized that AdminArea names in GoogleMaps are written in lower case (at least in Poland) and I decided that they would look better starting with capital letter in my new (very simple) app. Does this piece of code make sense? It works for me, but I am not sure if it's the best way to do that:

String firstLetter = listAddresses.get(0).getAdminArea().toUpperCase().charAt(0)+"";
String otherLetters = listAddresses.get(0).getAdminArea().substring(1);
String adminArea = firstLetter+otherLetters;
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Welcome to Stack Overflow. I suggest you visit our sister site [CodeReview.SE] for feedback on working code. Please be sure to read their documentation and follow their guidelines for posting questions. – Code-Apprentice Jan 07 '19 at 22:03

1 Answers1

1

Consider: org.apache.commons.lang3.StringUtils.capitalize:

public static String capitalize(final String str) {
    int strLen;
    if (str == null || (strLen = str.length()) == 0) {
        return str;
    }

    final char firstChar = str.charAt(0);
    final char newChar = Character.toTitleCase(firstChar);
    if (firstChar == newChar) {
        // already capitalized
        return str;
    }

    char[] newChars = new char[strLen];
    newChars[0] = newChar;
    str.getChars(1,strLen, newChars, 1);
    return String.valueOf(newChars);
}
Duane
  • 281
  • 1
  • 5