How to capitalize the first letter of word in a string using Java?

前端 未结 25 1514
抹茶落季
抹茶落季 2020-11-27 09:50

Example strings

one thousand only
two hundred
twenty
seven

How do I change the first character of a string in capital letter and not change

25条回答
  •  情话喂你
    2020-11-27 10:48

    If you only want to capitalize the first letter of a string named input and leave the rest alone:

    String output = input.substring(0, 1).toUpperCase() + input.substring(1);
    

    Now output will have what you want. Check that your input is at least one character long before using this, otherwise you'll get an exception.

提交回复
热议问题