Mega Code Archive

 
Categories / Android / Date Type
 

Converts a string to title casing

//package com.joulespersecond.seattlebusbot; public final class MyTextUtils {     /**      * Converts a string to title casing.      * @param str      *      The string to convert.      * @return      *      The converted string.      */     public static String toTitleCase(String str) {         if (str == null) {             return null;         }         boolean space = true;         StringBuilder builder = new StringBuilder(str);         final int len = builder.length();         for (int i=0; i < len; ++i) {             char c = builder.charAt(i);             if (space) {                 if (!Character.isWhitespace(c)) {                     // Convert to title case and switch out of whitespace mode.                     builder.setCharAt(i, Character.toTitleCase(c));                     space = false;                 }             } else if (Character.isWhitespace(c)) {                 space = true;             } else {                 builder.setCharAt(i, Character.toLowerCase(c));             }         }         return builder.toString();     } }