Java String Capitalize capitalize(String var)

Here you can find the source of capitalize(String var)

Description

capitalize

License

Apache License

Declaration

public static String capitalize(String var) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String capitalize(String var) {
        return capitalize(var, false);
    }//w  w w .  j  av a  2  s. co  m

    public static String capitalize(String var, boolean camelCaseUnderScore) {
        if (var == null)
            return null;
        if (camelCaseUnderScore) {
            var = camelCaseUnderscores(var);
        }
        String char1 = var.substring(0, 1);
        String rest = var.substring(1, var.length());
        char1 = char1.toUpperCase();
        return char1 + rest;
    }

    public static String camelCaseUnderscores(String str) {
        StringBuffer buff = new StringBuffer();
        boolean capitalize = false;
        boolean doAppend = false;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (capitalize) {
                ch = Character.toUpperCase(ch);
                doAppend = true;
            }
            if (ch == '_') {
                capitalize = true;
                doAppend = false;
            } else {
                capitalize = false;
                doAppend = true;
            }
            if (doAppend) {
                buff.append(ch);
            }

        }
        return buff.toString();
    }
}

Related

  1. Capitalize(String val)
  2. capitalize(String value)
  3. capitalize(String value)
  4. capitalize(String value)
  5. capitalize(String value)
  6. capitalize(String word)
  7. capitalize(String word)
  8. capitalize(String word)
  9. capitalize(String word)