Creating Displayable Text

Working on cycling through the fields of an object, and programmatically building a user friendly display text for each. To simplify my life, I’m using reflection, and the following method to capitalize the field name, and insert spaces appropriately (for camel-case field names):

private String createDisplay(String name) {
   for ( char c = 'A'; c <= 'Z'; c++ ) {
      name = name.replaceAll("" + c, " " + c);
   }

   // capitalize the first character
   name = Character.toTitleCase(name.charAt(0)) + name.substring(1);
   return name;
}

Leave a Reply

Your email address will not be published. Required fields are marked *