Java Uppercase First Letter

Uppercase the first letters in Strings. Change the first letter only or first letters in all words.
Uppercase first letters. A String needs an "Uppercase" first letter. It may contain two or more words—these also may need to be capitalized.Strings
A custom method. On the Java String class, we have toUpperCase, but this acts on all letters. We can use toCharArray and Character.toUpperCase to capitalize only certain letters.
Simple method. Let us start with a simple implementation. We introduce upperCaseFirst. This method receives a String and converts it to a char array with toCharArray.Char Arrays

Character: We use the Character class and the toUpperCase method to uppercase the first element in the char array.

Finally: We use the String constructor to convert our modified char array back into a String.

Char Array, String
Java program that uppercases first letter public class Program { public static String upperCaseFirst(String value) { // Convert String to char array. char[] array = value.toCharArray(); // Modify first element in array. array[0] = Character.toUpperCase(array[0]); // Return string. return new String(array); } public static void main(String[] args) { String value = "carrot"; String value2 = "steamed carrot"; // Test the upperCaseFirst method. String result = upperCaseFirst(value); System.out.println(value); System.out.println(result); result = upperCaseFirst(value2); System.out.println(value2); System.out.println(result); } } Output carrot Carrot steamed carrot Steamed carrot
Capitalize all words. This method, upperCaseAllFirst, is a more complex version of the previous method. It also uppercases the String's first letter.

But: It then searches the String for additional words. It uses Character.isWhitespace to find word breaks.

And: It invokes Character.toUpperCase to capitalize non-first words in the String.

Java program that uppercases all words public class Program { public static String upperCaseAllFirst(String value) { char[] array = value.toCharArray(); // Uppercase first letter. array[0] = Character.toUpperCase(array[0]); // Uppercase all letters that follow a whitespace character. for (int i = 1; i < array.length; i++) { if (Character.isWhitespace(array[i - 1])) { array[i] = Character.toUpperCase(array[i]); } } // Result. return new String(array); } public static void main(String[] args) { String value = "cat 123 456"; String value2 = "one two three"; // Test our code. String result = upperCaseAllFirst(value); System.out.println(value); System.out.println(result); result = upperCaseAllFirst(value2); System.out.println(value2); System.out.println(result); } } Output cat 123 456 Cat 123 456 one two three One Two Three
Results. To modify a String's individual letters, we can use toCharArray to get a char array. Then we can change letters in any way. We can test with Character methods.Character

And: An algorithm that capitalizes words can become complicated. We keep the logic simple, but it will not work in all cases.

Caution: For words with hyphens, like names ("Boyer-Moore" for example) the logic will not capitalize the second word.

Further: For short words like "A" the word will always be capitalized, even if this is not appropriate.

For ideal capitalization, a dictionary and lookup table (or directed acyclic graph) would need to be used. This problem rapidly becomes complex.

Often: Some special-casing logic in our code is sufficient to fix many small issues. Sometimes even this is not required.

A review. We developed a simple approach to capitalizing certain letters in Java Strings. We can use a for-loop to locate optimal characters to uppercase.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to