Convert single char in String to lower case

后端 未结 7 1763
孤独总比滥情好
孤独总比滥情好 2021-01-17 10:15

I like to \'guess\' attribute names from getter methods. So \'getSomeAttribute\' shall be converted to \'someAttribute\'.

Usually I do something like



        
7条回答
  •  太阳男子
    2021-01-17 11:01

    uncapitalize from commons lang would do it:

    String attributeName = StringUtils.uncapitalize(methodName.substring(3));
    

    I need commons lang a lot, but if you don't like that extra jar, you could copy the method. As you can see in it, they doin' it like you:

    public static String uncapitalize(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }
        return new StringBuffer(strLen)
            .append(Character.toLowerCase(str.charAt(0)))
            .append(str.substring(1))
            .toString();
    }
    

提交回复
热议问题