What is the method for capitalizing the first letter in Java?
In Java, to capitalize the first letter of a string, you can use the substring function and the toUpperCase function. The specific steps are as follows:
- a portion of a larger string
- “hi”
- Hello
- convert the text to upper case
- Could you please pass me the letter “h”?
- The eighth letter of the alphabet is “H”.
- Combine the remaining part with the converted first letter.
Here is an example code:
public class Main {
public static void main(String[] args) {
String str = "hello";
String result = str.substring(0, 1).toUpperCase() + str.substring(1);
System.out.println(result); // 输出 "Hello"
}
}