将String中的单个char转换为小写

时间:2023-01-25 09:22:43

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

我喜欢从getter方法中“猜测”属性名称。因此'getSomeAttribute'将转换为'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?

很难看,对吧?我通常把它隐藏在一个方法中,但有人知道更好的解决方案吗?

7 个解决方案

#1


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

我认为你的解决方案很好。我不认为有任何更简单的方法来做到这一点。

#2


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

Commons Lang的非资本化方法可以帮助你,但我不认为你的解决方案是如此粗糙。

#3


Have a look at the JavaBeans API:

看看JavaBeans API:

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

Also see decapitalize.

另见decapitalize。

#4


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();
}

#5


Its worth remembering that;

值得记住的是;

  • not all getXXX methods are getters e.g. double getSqrt(double x), void getup().
  • 并非所有getXXX方法都是getter,例如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().
  • 返回boolean,以is开头并且不带参数的方法可以是getter,例如boolean isActive()。

#6


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

经测试和功能:)

#7


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.

看起来很好。是的,它看起来很冗长,但考虑一下你正在尝试做什么,以及另一个程序员如果想要了解这些代码试图做什么会想到什么。如果有的话,我会通过添加您正在做的事情(从getter方法猜测属性名称)作为注释来延长它。

#1


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

我认为你的解决方案很好。我不认为有任何更简单的方法来做到这一点。

#2


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

Commons Lang的非资本化方法可以帮助你,但我不认为你的解决方案是如此粗糙。

#3


Have a look at the JavaBeans API:

看看JavaBeans API:

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

Also see decapitalize.

另见decapitalize。

#4


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();
}

#5


Its worth remembering that;

值得记住的是;

  • not all getXXX methods are getters e.g. double getSqrt(double x), void getup().
  • 并非所有getXXX方法都是getter,例如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().
  • 返回boolean,以is开头并且不带参数的方法可以是getter,例如boolean isActive()。

#6


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

经测试和功能:)

#7


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.

看起来很好。是的,它看起来很冗长,但考虑一下你正在尝试做什么,以及另一个程序员如果想要了解这些代码试图做什么会想到什么。如果有的话,我会通过添加您正在做的事情(从getter方法猜测属性名称)作为注释来延长它。