String (String name) { // Either last uppercase letter (if it's not the last letter) or the first letter, removing leading underscores // token -> t // myAccessToken -> t // SQL -> s // __name -> n // __1 -> k String nameCandidate; name = name.replaceFirst("^[_\\d]+", ""); if (name.isEmpty()) return "k"; nameCandidate = name.substring(0, 1); for (int pos = name.length() - 1; pos > 0; pos--) { if (Character.isUpperCase(name.charAt(pos))) { if (pos != name.length() - 1) { nameCandidate = name.substring(pos, pos + 1); } break; } } return StringUtil.toLowerCase(nameCandidate); }