问题描述
制作 String
小写字母的第一个字符的最有效方法是什么?
What is the most efficient way to make the first character of a String
lower case?
我可以想一想这样做的几种方法:
I can think of a number of ways to do this:
使用 charAt()
带 substring()
String input = "SomeInputString";
String output = Character.toLowerCase(input.charAt(0)) +
(input.length() > 1 ? input.substring(1) : "");
或使用 char
array
String input = "SomeInputString";
char c[] = input.toCharArray();
c[0] = Character.toLowerCase(c[0]);
String output = new String(c);
我相信还有很多其他好方法可以实现这一点。你推荐什么?
I am sure there are many other great ways to achieve this. What do you recommend?
推荐答案
我使用。完整基准。
期间的假设测试(以避免每次检查转角情况):输入字符串长度始终大于1.
Assumption during the tests (to avoid checking the corner cases every time): the input String length is always greater than 1.
Benchmark Mode Cnt Score Error Units
MyBenchmark.test1 thrpt 20 10463220.493 ± 288805.068 ops/s
MyBenchmark.test2 thrpt 20 14730158.709 ± 530444.444 ops/s
MyBenchmark.test3 thrpt 20 16079551.751 ± 56884.357 ops/s
MyBenchmark.test4 thrpt 20 9762578.446 ± 584316.582 ops/s
MyBenchmark.test5 thrpt 20 6093216.066 ± 180062.872 ops/s
MyBenchmark.test6 thrpt 20 2104102.578 ± 18705.805 ops/s
分数是每秒操作数越多越好。
The score are operations per second, the more the better.
-
test1
首先是Andy和Hllink的方法:
test1
was first Andy's and Hllink's approach:
string = Character.toLowerCase(string.charAt(0)) + string.substring(1);
test2
是第二个Andy的方法。它也是,但如果没有两个
语句。由于测试假设,第一个如果
被删除。第二个被删除,因为它违反了正确性(即输入HI
将返回HI
)。这几乎是最快的。
test2
was second Andy's approach. It is also Introspector.decapitalize()
suggested by Daniel, but without two if
statements. First if
was removed because of the testing assumption. The second one was removed, because it was violating correctness (i.e. input "HI"
would return "HI"
). This was almost the fastest.
char c[] = string.toCharArray();
c[0] = Character.toLowerCase(c[0]);
string = new String(c);
test3
是对 test2
,但不是 Character.toLowerCase()
,而是添加了32,当且仅当string是ASCII格式。这是最快的。来自Mike的 c [0] | =''
给出了相同的表现。
test3
was a modification of test2
, but instead of Character.toLowerCase()
, I was adding 32, which works correctly if and only if the string is in ASCII. This was the fastest. c[0] |= ' '
from Mike's comment gave the same performance.
char c[] = string.toCharArray();
c[0] += 32;
string = new String(c);
test4
使用 StringBuilder
。
StringBuilder sb = new StringBuilder(string);
sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
string = sb.toString();
test5
使用两个 substring()
调用。
string = string.substring(0, 1).toLowerCase() + string.substring(1);
test6
使用反射来改变直接在String中。这是最慢的。
test6
uses reflection to change char value[]
directly in String. This was the slowest.
try {
Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
char[] value = (char[]) field.get(string);
value[0] = Character.toLowerCase(value[0]);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
结论
如果字符串长度始终大于0,请使用 test2
。
如果没有,我们必须检查角落情况:
If not, we have to check the corner cases:
public static String decapitalize(String string)
if (string == null || string.length() == 0) {
return string;
}
char c[] = string.toCharArray();
c[0] = Character.toLowerCase(c[0]);
return new String(c);
}
如果你确定你的文字总是用ASCII而你正在寻找为了获得极高的性能,因为您发现此代码存在瓶颈,请使用 test3
。
If you are sure that your text will be always in ASCII and you are looking for extreme performance because you found this code in the bottleneck, use test3
.
这篇关于最有效的方法是使字符串小写的第一个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!