Commit 4b7a188e authored by Nico Mack's avatar Nico Mack

added toTitleCase method to StringUtils class

parent f35589d9
......@@ -37,6 +37,8 @@ public class StringUtils {
private static final Pattern ESCAPED_PATTERN = Pattern.compile("\\\\([{}])", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Pattern TITLE_CASE_PATTERN = Pattern.compile("\\b([a-z]).*?", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
private static final Logger LOGGER = LoggerFactory.getLogger(StringUtils.class.getSimpleName());
// ---------------------------------------------------------------------------
......@@ -108,4 +110,30 @@ public class StringUtils {
return builder.toString();
}
// ---------------------------------------------------------------------------
/**
* @param original
* @return
*/
// ---------------------------------------------------------------------------
public static String toTitleCase(String original) {
StringBuilder builder = new StringBuilder();
Matcher titleMatcher = TITLE_CASE_PATTERN.matcher(original);
int position = 0;
while (titleMatcher.find()) {
builder.append(original.substring(position, titleMatcher.start()));
builder.append(titleMatcher.group(1).toUpperCase());
position = titleMatcher.end();
}
builder.append(original.substring(position));
return builder.toString();
}
// ---------------------------------------------------------------------------
}
......@@ -68,7 +68,13 @@ public class StringUtilsTest {
completed = StringUtils.build("Careful with that axe eugene!", param2);
assertEquals("Careful with that axe eugene!", completed);
}
@Test
public void testTitleCase() {
String original = "what the @ has this 2 do with me"; //$NON-NLS-1$
String titleCase = StringUtils.toTitleCase(original);
assertEquals("What The @ Has This 2 Do With Me", titleCase);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment