substring()
and toUpperCase()
StringBuilder
In Java, strings are immutable, which means once a string object is created, its value cannot be changed. To convert the first letter of a string to uppercase, we need to create a new string that contains the modified text. The basic idea is to extract the first character of the string, convert it to uppercase, and then concatenate it with the rest of the string.
substring()
and toUpperCase()
public class FirstLetterToCaps {
public static String capitalizeFirstLetter(String input) {
if (input == null || input.isEmpty()) {
return input;
}
// Extract the first character and convert it to uppercase
String firstChar = input.substring(0, 1).toUpperCase();
// Get the rest of the string
String remainingChars = input.substring(1);
// Concatenate the capitalized first character with the rest of the string
return firstChar + remainingChars;
}
public static void main(String[] args) {
String str = "hello world";
String capitalizedStr = capitalizeFirstLetter(str);
System.out.println("Original string: " + str);
System.out.println("Capitalized string: " + capitalizedStr);
}
}
In this example, we first check if the input string is null or empty. If it is, we return the input as it is. Otherwise, we extract the first character using the substring()
method, convert it to uppercase using the toUpperCase()
method, and then concatenate it with the rest of the string.
StringBuilder
public class FirstLetterToCapsUsingStringBuilder {
public static String capitalizeFirstLetter(String input) {
if (input == null || input.isEmpty()) {
return input;
}
StringBuilder sb = new StringBuilder(input);
// Convert the first character to uppercase
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
return sb.toString();
}
public static void main(String[] args) {
String str = "hello world";
String capitalizedStr = capitalizeFirstLetter(str);
System.out.println("Original string: " + str);
System.out.println("Capitalized string: " + capitalizedStr);
}
}
Here, we use a StringBuilder
object to build the modified string. We first check if the input string is null or empty. If not, we create a StringBuilder
object from the input string and use the setCharAt()
method to convert the first character to uppercase. Finally, we convert the StringBuilder
object back to a string using the toString()
method.
NullPointerException
or get unexpected results.+
operator can be inefficient, especially when dealing with large strings. Using StringBuilder
can improve performance.toUpperCase()
method uses the default locale. If you need to support different locales, you may need to specify the locale explicitly.StringBuilder
for Concatenation: When concatenating strings, especially in a loop, use StringBuilder
to improve performance.toUpperCase()
method that accepts a locale parameter.Converting the first letter of a string to uppercase is a common operation in Java. By understanding the core concepts, typical usage scenarios, and following best practices, you can write efficient and robust code to achieve this. Whether you choose to use the substring()
and toUpperCase()
methods or a StringBuilder
, make sure to handle null and empty strings properly and consider performance and locale issues.
+
operator to concatenate strings instead of StringBuilder
?+
operator, but it can be inefficient, especially when dealing with large strings or performing multiple concatenations. StringBuilder
is recommended for better performance.split()
method, capitalize the first letter of each word, and then join the words back together.