capitalize string via Character.toUpperCase() - Android java.lang

Android examples for java.lang:String Case

Description

capitalize string via Character.toUpperCase()

Demo Code

import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;

public class Main{

    public static String capitalize(String str) {

        StringBuilder sb = new StringBuilder(str);

        for (int i = 0; i < sb.length(); i++) {
            if (i == 0 || Character.isWhitespace(sb.charAt(i - 1))) {
                sb.replace(i, i + 1, Character.toString(Character
                        .toUpperCase(sb.charAt(i))));
            }/*from   www .  j  a va  2 s. c  o  m*/
        }

        return sb.toString();
    }

}

Related Tutorials