どうも、ちょげ(@chogetarou)です。
文字列(string)の先頭を大文字に変換する方法を紹介します。
方法

文字列(string)の先頭を大文字に変換するには、substring()を使います。
まず、「+」の左辺と右辺で文字列からsubstring()を呼び出します。
左辺のsubstring()の第1引数に0、第2引数に1を指定します。
また、左辺のsubstring()からtoUpperCase()を呼び出します。
右辺のsubstring()の引数に1を指定します。
String result = text.substring(0,1).toUpperCase() + text.substring(1);
上記の「+」は、substring()を呼び出した文字列の先頭を大文字に変換した文字列を返します。
使用例
public class Main {
public static void main(String[] args) throws Exception {
String text = "hello,world";
String result = text.substring(0,1).toUpperCase() + text.substring(1);
System.out.println(text);
System.out.println(result);
}
}
出力:
hello,world
Hello,world
コメント