Java String Capitalize All capitalizeAllFirstChar(String text)

Here you can find the source of capitalizeAllFirstChar(String text)

Description

Sample1: S E S submit date -> S E S Submit Date

License

Open Source License

Parameter

Parameter Description
text a parameter

Declaration

public static String capitalizeAllFirstChar(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w  w.ja  v a2 s .co  m
     * Sample1: S E S submit date -> S E S Submit Date
     * @param text
     * @return
     */
    public static String capitalizeAllFirstChar(String text) {
        if (text != null) {
            StringBuilder builder = new StringBuilder(text);
            for (int i = 0, count = builder.length(); i < count; i++) {
                char c = builder.charAt(i);
                if (i == 0) {
                    if (c >= 97 && c <= 122) {
                        builder.deleteCharAt(i);
                        builder.insert(i, Character.toUpperCase(c));
                    }
                } else if (c == ' ' && i < count - 1) {
                    int j = i + 1;
                    char r = builder.charAt(j);
                    if (r >= 97 && r <= 122) {
                        builder.deleteCharAt(j);
                        builder.insert(j, Character.toUpperCase(r));
                    }
                }
            }
            return builder.toString();
        }
        return text;
    }
}

Related

  1. capitaliseAllWords(String str)
  2. capitaliseAllWords(String str)
  3. capitalizeAll(final String value)
  4. capitalizeAll(String string)
  5. capitalizeAll(String text)
  6. capitalizeAllWords(String text)
  7. capitalizeAllWords(String words)