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:

  1. a portion of a larger string
  2. “hi”
  3. Hello
  4. convert the text to upper case
  5. Could you please pass me the letter “h”?
  6. The eighth letter of the alphabet is “H”.
  7. 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"
    }
}
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds