protected void trim(StringBuilder text) { if (!trim) return; while (text.length() > 0 && MString.isWhitespace(text.charAt(0)) ) text.deleteCharAt(0); while (text.length() > 0 && MString.isWhitespace(text.charAt(text.length()-1)) ) text.deleteCharAt(text.length()-1); }
/** * Try to find initials out of the given title. Use the first letters after whitspaces for the initials. The * returned string is not in upper letters. If you need Upper Letters append toUpper(). * @param title The input String * @param upperOnly Set true if only collect upper characters in title * @param digits Set true if digits (after whitespaces) should also be used. * @param maxSize Max size in characters of the initilas or 0 for no limit. * @return The initials in mixed letters. */ public static String findInitials(String title, boolean upperOnly, boolean digits, int maxSize) { if (isEmptyTrim(title)) return "?"; StringBuilder out = new StringBuilder(); // find first letters boolean isSpace = true; for (int i = 0; i < title.length(); i++) { char c = title.charAt(i); if (isWhitespace(c)) { isSpace = true; } else { if (isSpace) { if ((c >= 'A' && c <= 'Z') || !upperOnly && ( c >= 'a' && c <= 'z') || digits && (c >= '0' && c <= '9')) out.append(c); if (maxSize > 0 && out.length() >= maxSize) break; } isSpace = false; } } return out.toString(); }