VistaStringUtils { public static String nameCase(String s) { if (s == null) return null; s = replaceAll(WORD_WITH_NON_NUMERIC_FIRST_LETTER, s, new Callback() { public String foundMatch(MatchResult match) { return match.group().toLowerCase(); } }); s = replaceAll(FIRST_LETTER, s, new Callback() { public String foundMatch(MatchResult match) { return match.group().toUpperCase(); } }); s = replaceAll(APOSTROPHE, s, new Callback() { public String foundMatch(MatchResult match) { return match.group().toLowerCase(); } }); if (MAC.matcher(s).find() || MC.matcher(s).find()) { s = replaceAll(Pattern.compile("\\b(Ma?c)([A-Za-z]+)"), s, new Callback() { public String foundMatch(MatchResult match) { return match.group(1) + StringUtils.capitalize(match.group(2)); } }); s = s.replaceAll("\\bMacEvicius", "Macevicius"); s = s.replaceAll("\\bMacHado", "Machado"); s = s.replaceAll("\\bMacHar", "Machar"); s = s.replaceAll("\\bMacHin", "Machin"); s = s.replaceAll("\\bMacHlin", "Machlin"); s = s.replaceAll("\\bMacIas", "Macias"); s = s.replaceAll("\\bMacIej", "Maciej"); s = s.replaceAll("\\bMacIek", "Maciek"); s = s.replaceAll("\\bMacIulis", "Maciulis"); s = s.replaceAll("\\bMacKie", "Mackie"); s = s.replaceAll("\\bMacKle", "Mackle"); s = s.replaceAll("\\bMacKlin", "Macklin"); s = s.replaceAll("\\bMacQuarie", "Macquarie"); } s = s.replace("Macmurdo", "MacMurdo"); s = s.replaceAll("\\bAl(?=\\s+\\w)", "al"); s = s.replaceAll("\\bAp\\b", "ap"); s = s.replaceAll("\\bBen(?=\\s+\\w)", "ben"); s = s.replaceAll("\\bDell([ae])\\b", "dell$1"); s = s.replaceAll("\\bD([aeiou])\\b", "d$1"); s = s.replaceAll("\\bD([ao]s)\\b", "d$1"); s = s.replaceAll("\\bDe([lr])\\b", "de$1"); s = s.replaceAll("\\bEl\\b", "el"); s = s.replaceAll("\\bLa\\b", "la"); s = s.replaceAll("\\bL([eo])\\b", "l$1"); s = s.replaceAll("\\bVan(?=\\s+\\w)", "van"); s = s.replaceAll("\\bVon\\b", "von"); s = replaceAll(ROMAN_NUMERAL, s, new Callback() { public String foundMatch(MatchResult match) { return match.group().toUpperCase(); } }); return s; }  static String piece(String s); static String piece(String s, int piece1); static String piece(String s, int piece1, int piece2); static String piece(String s, String delimiter); static String piece(String s, String delimiter, int piece1); static String piece(String s, String delimiter, int piece1, int piece2); static String translate(String s, String identifier, String associator); static long crc16(String s); static String crc16Hex(String s); static Object splitLargeStringIfNecessary(String s); static Object splitLargeStringIfNecessary(String s, int length); static String nameCase(String s); static final String U; }
@Test public void testNameCase() { String[] properNames = new String[] { "Keith", "Leigh-Williams", "McCarthy", "O'Callaghan", "St. John", "von Streit", "van Dyke", "Van", "ap Llwyd Dafydd", "al Fahd", "Al", "el Grecco", "ben Gurion", "Ben", "da Vinci", "di Caprio", "du Pont", "de Legate", "del Crond", "der Sind", "van der Post", "von Trapp", "la Poisson", "le Figaro", "Mack Knife", "Dougal MacDonald", "Machin", "Machlin", "Machar", "Mackle", "Macklin", "Mackie", "Macquarie", "Machado", "Macevicius", "Maciulis", "Macias", "MacMurdo", "Maciej", "Maciek", "Henry VIII", "Louis III", "Louis XIV", "Charles II", "Fred XLIX", "3E North", "3 North Surg", "7B", "2-Intermed" }; for(String name : properNames) { assertThat(nameCase(name), is(equalTo(name))); } }
@Test public void testNameCaseMultibyte() { String properCased = "Iñtërnâtiônàlizætiøn"; assertThat(nameCase(properCased), is(equalTo(properCased))); }
@Test public void testNameCaseNullSafe() { assertThat(nameCase(null), nullValue()); }