Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert single char in String to lower case

Tags:

java

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

Usually I do something like

String attributeName = Character.toLowerCase(methodName.indexOf(3)) 
                       + methodName.substring(4);

Pretty ugly, right? I usually hide it in a method, but does anybody know a better solution?

like image 343
Andreas Dolk Avatar asked May 12 '09 14:05

Andreas Dolk


People also ask

How do I convert a char to lowercase?

The java. lang. Character. toLowerCase(char ch) converts the character argument to lowercase using case mapping information from the UnicodeData file.

How do I convert a string to all lowercase?

The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.

How do you convert a single character to lowercase in python?

In Python, lower() is a built-in method used for string handling. The lower() methods returns the lowercased string from the given string. It converts all uppercase characters to lowercase. If no uppercase characters exist, it returns the original string.


5 Answers

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

like image 197
Valentin Rocher Avatar answered Oct 03 '22 02:10

Valentin Rocher


Have a look at the JavaBeans API:

BeanInfo info = Introspector.getBeanInfo(bean
       .getClass(), Object.class);
for (PropertyDescriptor propertyDesc : info
       .getPropertyDescriptors()) {
  String name = propertyDesc.getName();
}

Also see decapitalize.

like image 34
McDowell Avatar answered Oct 03 '22 01:10

McDowell


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();
}
like image 35
Tim Büthe Avatar answered Oct 03 '22 02:10

Tim Büthe


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().
like image 36
Peter Lawrey Avatar answered Oct 03 '22 00:10

Peter Lawrey


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 :)

like image 42
DeadDingo Avatar answered Oct 03 '22 02:10

DeadDingo