capitalize Each Word by locale - Android java.lang

Android examples for java.lang:String Case

Description

capitalize Each Word by locale

Demo Code


public class Main{

    public static String capitalizeEachWord(final String text,
            final String separators, final Locale locale) {
        final StringBuilder builder = new StringBuilder();
        boolean needCapsNext = true;
        final int len = text.length();
        for (int i = 0; i < len; i = text.offsetByCodePoints(i, 1)) {
            final String nextChar = text.substring(i,
                    text.offsetByCodePoints(i, 1));
            if (needCapsNext) {
                builder.append(nextChar.toUpperCase(locale));
            } else {
                builder.append(nextChar.toLowerCase(locale));
            }/*  w ww .  j  a va 2s.c o m*/
            // We need a capital letter next if this is a separator.
            needCapsNext = (-1 != separators.indexOf(nextChar
                    .codePointAt(0)));
        }
        return builder.toString();
    }

}

Related Tutorials