Convert single char in String to lower case

后端 未结 7 1753
孤独总比滥情好
孤独总比滥情好 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 10:57

    Its worth remembering that;

    • not all getXXX methods are getters e.g. double getSqrt(double x), void getup().
    • methods which return boolean, start with is and don't take an argument can be a getter, e.g. boolean isActive().
    0 讨论(0)
  • 2021-01-17 10:57

    Given a character buffer, you can apply the below code:

    int i = 0;
    for(char x : buffer) {
        buffer[i] = Character.toLowerCase(x);
        i++;
    }
    

    Tested and functions :)

    0 讨论(0)
  • 2021-01-17 11:01

    I think your solution is just fine. I dont think there is any easier way to do it.

    0 讨论(0)
  • 2021-01-17 11:01

    The uncapitalize method of Commons Lang shall help you, but I don't think your solution is so crude.

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-01-17 11:10

    Looks fine to me. Yes, it looks verbose, but consider what you're trying to do, and what another programmer would think if they were trying to understand what this code is trying to do. If anything, I'd make it longer, by adding what you're doing (guessing attribute names from getter methods) as a comment.

    0 讨论(0)
提交回复
热议问题