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

前端 未结 11 1787
孤街浪徒
孤街浪徒 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:19

    For me none of working:

    Function:

    private String getCapsSentences(String tagName) {
        String[] splits = tagName.toLowerCase().split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < splits.length; i++) {
            String eachWord = splits[i];
            if (i > 0 && eachWord.length() > 0) {
                sb.append(" ");
            }
            String cap = eachWord.substring(0, 1).toUpperCase() 
                    + eachWord.substring(1);
            sb.append(cap);
        }
        return sb.toString();
    }
    

    Result:

    I/P brain O/P Brain

    I/P Brain and Health O/P Brain And Health

    I/P brain And health to O/P Brain And Health

    I/P brain's Health to O/P Brain's Health

    I/P brain's Health and leg to O/P Brain's Health And Leg

    Hope this would help you.

提交回复
热议问题