capitalize First Letter by regular expression - Android java.util.regex

Android examples for java.util.regex:Character Pattern

Description

capitalize First Letter by regular expression

Demo Code

import android.util.Log;
import java.io.UnsupportedEncodingException;

public class Main{

    public static String capitalizeFirstLetter(String input) {
        input = input.toLowerCase();//from   w  ww  . j  a v a  2  s  . co  m
        String[] words = input.split("\\s+");
        StringBuilder sb = new StringBuilder(input.length());
        if (words[0].length() > 0) {
            sb.append(Character.toUpperCase(words[0].charAt(0))).append(
                    words[0].substring(1, words[0].length()));
            for (int ii = 1; ii < words.length; ii++) {
                sb.append(" ");
                sb.append(Character.toUpperCase(words[ii].charAt(0)))
                        .append(words[ii].substring(1, words[ii].length()));
            }
        }
        return sb.toString();
    }

}

Related Tutorials