Whitecat Whitecat - 1 year ago 98
Java Question

How do I convert underscore into human-readable names in Java?

I'd like to write a method that converts CamelCase into a human-readable name.
This is very similar to this question but now with underscore.

Here's the test case:

public void testSplitUnderscore() {
assertEquals("lowercase", splitUnderscore("lowercase"));
assertEquals("Class", splitUnderscore("Class"));
assertEquals("My Class", splitUnderscore("My_Class"));
assertEquals("HTML", splitUnderscore("HTML"));
assertEquals("PDF Loader", splitUnderscore("PDF_Loader"));
assertEquals("A String", splitUnderscore("A_String"));
assertEquals("Simple XML Parser", splitUnderscore("Simple_XML_Parser"));
assertEquals("GL 11 Version", splitUnderscore("GL_11_Version"));
assertEquals("99 Bottles", splitUnderscore("99_Bottle"));
assertEquals("May 5", splitUnderscore("May_5"));
assertEquals("BFG 9000", splitUnderscore("BFG_9000"));
assertEquals("beginning", splitUnderscore("_beginning"));
assertEquals("end", splitUnderscore("end_"));

Answer Source
public static String splitUnderscore(String s) {
    return s.replace("_", " ").trim();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download