How to capitalize the first letter of text in a TextView in an Android Application

前端 未结 11 1774
孤街浪徒
孤街浪徒 2020-12-23 15:35

I\'m not referring to textInput, either. I mean that once you have static text in a TextView (populated from a Database call to user inputted data (that may not be Capitaliz

11条回答
  •  遥遥无期
    2020-12-23 16:20

    The accepted answer is good, but if you are using it to get values from a textView in android, it would be good to check if the string is empty. If the string is empty it would throw an exception.

    private String capitizeString(String name){
        String captilizedString="";
        if(!name.trim().equals("")){
           captilizedString = name.substring(0,1).toUpperCase() + name.substring(1);
        }
        return captilizedString;
    }
    

提交回复
热议问题