I Spy Code - Java

Capitalize the first character in a string

Question:

Write a java program that capitalizes the first character in a string.

Solution:

Here is a java example that capitalizes the first character in a string:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
    public static void main(String[] args) {
        String str = "the cat in the hat";
        System.out.println(str);
 
        String cap = StringUtils.capitalize(str);
        System.out.println(cap);
    }
 
}
 

Output:

$ java Example
the cat in the hat
The cat in the hat

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#capitalize-java.lang.String-

Questions answered by this page:

How to capitalize the first letter of a String in Java
Capitalizing the first character of a string with Java