Uppercase first letters. A String needs an "Uppercase" first letter. It may contain two or more words—these also may need to be capitalized.
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.
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);
}
}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.
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);
}
}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.
And An algorithm that capitalizes words can become complicated. We keep the logic simple, but it will not work in all cases.
Warning 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.
Detail 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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.