Example usage for org.apache.commons.lang3 ExtendedCharSequenceUtils capitalize

List of usage examples for org.apache.commons.lang3 ExtendedCharSequenceUtils capitalize

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ExtendedCharSequenceUtils capitalize.

Prototype

public static final String capitalize(CharSequence cs) 

Source Link

Usage

From source file:net.community.chest.gitcloud.facade.ServletUtils.java

public static final String capitalizeHttpHeaderName(String hdrName) {
    if (StringUtils.isEmpty(hdrName)) {
        return hdrName;
    }/*from   w w  w  . ja  v  a 2s.c  om*/

    int curPos = hdrName.indexOf('-');
    if (curPos < 0) {
        return ExtendedCharSequenceUtils.capitalize(hdrName);
    }

    StringBuilder sb = null;
    for (int lastPos = 0;;) {
        char ch = hdrName.charAt(lastPos), tch = Character.toTitleCase(ch);
        if (ch != tch) {
            if (sb == null) {
                sb = new StringBuilder(hdrName.length());
                // append the data that was OK
                if (lastPos > 0) {
                    sb.append(hdrName.substring(0, lastPos));
                }
            }

            sb.append(tch);

            if (curPos > lastPos) {
                sb.append(hdrName.substring(lastPos + 1 /* excluding the capital letter */,
                        curPos + 1 /* including the '-' */));
            } else { // last component in string
                sb.append(hdrName.substring(lastPos + 1 /* excluding the capital letter */));
            }
        }

        if (curPos < lastPos) {
            break;
        }

        if ((lastPos = curPos + 1) >= hdrName.length()) {
            break;
        }

        curPos = hdrName.indexOf('-', lastPos);
    }

    if (sb == null) { // There was no need to modify anything
        return hdrName;
    } else {
        return sb.toString();
    }
}