Java 将字符串中的单个字符转换为小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/853017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert single char in String to lower case
提问by Andreas Dolk
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?
很丑吧?我通常将它隐藏在一种方法中,但是有人知道更好的解决方案吗?
采纳答案by Josh Curren
I think your solution is just fine. I dont think there is any easier way to do it.
我认为你的解决方案很好。我不认为有任何更简单的方法来做到这一点。
回答by Valentin Rocher
The uncapitalizemethod of Commons Lang shall help you, but I don't think your solution is so crude.
Commons Lang的uncapitalize方法会对您有所帮助,但我不认为您的解决方案如此粗糙。
回答by Tim Büthe
uncapitalizefrom commons langwould do it:
uncapitalizefrom commons lang会这样做:
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:
我非常需要 commons lang,但如果你不喜欢那个额外的 jar,你可以复制这个方法。正如您在其中看到的,他们像您一样做事:
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();
}
回答by Paul Brinkley
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 方法猜测属性名称)作为注释来延长它。
回答by McDowell
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.
另请参阅decapitalize。
回答by Peter Lawrey
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().
- 并非所有 getXXX 方法都是 getter,例如 double getSqrt(double x)、void getup()。
- 返回布尔值、以 is 开头且不带参数的方法可以是 getter,例如 boolean isActive()。
回答by DeadDingo
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 :)
测试和功能:)