Capitalize the first character of the given string in Java
Description
The following code shows how to capitalize the first character of the given string.
Example
/* w w w. j a v a2s . c om*/
public class Main {
public static void main(String[] argv) {
System.out.println(capitalize("this is a test from java2s.com"));
}
public static String capitalize(final String string) {
if (string == null)
throw new NullPointerException("string");
if (string.equals(""))
throw new NullPointerException("string");
return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »