Java String Capitalize First capitalizeFirstLetter(String s)

Here you can find the source of capitalizeFirstLetter(String s)

Description

Capitalize first letter in string.

License

Open Source License

Parameter

Parameter Description
s String to capitalize.

Return

"s" with first letter (not first character) capitalized. Remaining characters are set to lower case.

Declaration


public static String capitalizeFirstLetter(String s) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /**   Capitalize first letter in string.
     */* w w w.ja va2 s.com*/
     *   @param   s   String to capitalize.
     *
     *   @return      "s" with first letter (not first character) capitalized.
     *            Remaining characters are set to lower case.
     */

    public static String capitalizeFirstLetter(String s) {
        char[] chars = s.toLowerCase().toCharArray();

        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];

            if (Character.isLetter(ch)) {
                chars[i] = Character.toUpperCase(ch);
                break;
            }
        }

        return new String(chars);
    }

    /**   Check if character is a letter.
     *
     *   @param   c   Character to test.
     *
     *   @return      true if character is a letter.
     */

    public static boolean isLetter(char c) {
        return Character.isLetter(c);
    }

    /**   Check if string is a single letter.
     *
     *   @param   s   String to test.
     *
     *   @return      true if string is a single letter.
     */

    public static boolean isLetter(String s) {
        return (s != null) && (s.length() == 1) && Character.isLetter(s.charAt(0));
    }
}

Related

  1. capitalizeFirstLetter(String data)
  2. capitalizeFirstLetter(String data)
  3. capitalizeFirstLetter(String data)
  4. capitalizeFirstLetter(String message)
  5. CapitalizeFirstLetter(String name)
  6. capitalizeFirstLetter(String s)
  7. capitalizeFirstLetter(String s)
  8. capitalizeFirstLetter(String s)
  9. capitalizeFirstLetter(String s)