[英]Construct a string from a repeated character
In Java I need to construct a string of n
zeros with n
unknown at compile time. 在Java中,我需要在编译时构造一个
n
为零且n
未知的字符串。 Ideally I'd use 理想情况下,我会使用
String s = new String('0', n);
But no such constructor exists. 但是不存在这样的构造函数。
CharSequence
doesn't seem to have a suitable constructor either. CharSequence
似乎也没有合适的构造函数。 So I'm tempted to build my own loop using StringBuilder
. 所以我很想使用
StringBuilder
构建自己的循环。
Before I do this and risk getting defenestrated by my boss, could anyone advise: is there a standard way of doing this in Java? 在我这样做并冒被老板冒失的风险之前,有人可以建议:Java是否有标准的方法来做到这一点? In C++, one of the
std::string
constructors allows this. 在C ++中,
std::string
构造函数之一允许这样做。
If you don't mind creating an extra string: 如果您不介意创建额外的字符串:
String zeros = new String(new char[n]).replace((char) 0, '0');
Or more explicit (and probably more efficient): 或更明确(可能更有效):
char[] c = new char[n];
Arrays.fill(c, '0');
String zeros = new String(c);
Performance wise, the Arrays.fill
option seems to perform better in most situations, but especially for large strings. 在性能方面,
Arrays.fill
选项在大多数情况下似乎表现更好,但对于大型字符串尤其如此。 Using a StringBuilder
is quite slow for large strings but efficient for small ones. 对于大型字符串,使用
StringBuilder
相当慢,而对于小型字符串则使用高效。 Using replace
is a nice one liner and performs ok for larger strings, but not as well as filll
. 使用
replace
是一个很好的衬板,对于较大的字符串也可以执行,但不如filll
。
Micro benchmark for different values of n: 不同n值的微基准:
Benchmark (n) Mode Samples Score Error Units
c.a.p.SO26504151.builder 1 avgt 3 29.452 ± 1.849 ns/op
c.a.p.SO26504151.builder 10 avgt 3 51.641 ± 12.426 ns/op
c.a.p.SO26504151.builder 1000 avgt 3 2681.956 ± 336.353 ns/op
c.a.p.SO26504151.builder 1000000 avgt 3 3522995.218 ± 422579.979 ns/op
c.a.p.SO26504151.fill 1 avgt 3 30.255 ± 0.297 ns/op
c.a.p.SO26504151.fill 10 avgt 3 32.638 ± 7.553 ns/op
c.a.p.SO26504151.fill 1000 avgt 3 592.459 ± 91.413 ns/op
c.a.p.SO26504151.fill 1000000 avgt 3 706187.003 ± 152774.601 ns/op
c.a.p.SO26504151.replace 1 avgt 3 44.366 ± 5.153 ns/op
c.a.p.SO26504151.replace 10 avgt 3 51.778 ± 2.959 ns/op
c.a.p.SO26504151.replace 1000 avgt 3 1385.383 ± 289.319 ns/op
c.a.p.SO26504151.replace 1000000 avgt 3 1486335.886 ± 1807239.775 ns/op
Create a n
sized char array and convert it to String: 创建一个
n
大小的char数组并将其转换为String:
char[] myZeroCharArray = new char[n];
for(int i = 0; i < n; i++) myZeroCharArray[i] = '0';
String myZeroString = new String(myZeroCharArray);
See StringUtils in Apache Commons Lang 请参阅Apache Commons Lang中的StringUtils
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#repeat%28java.lang.String,%20int%29 https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#repeat%28java.lang.String,%20int%29
没有标准的JDK方式,但是Apache通用(几乎是事实上的标准)具有StringUtils.repeat()方法,例如:
String s = StringUtils.repeat('x', 5); // s = "xxxxx"
or the plain old String Format 或普通的旧字符串格式
int n = 10;
String s = String.format("%" + n + "s", "").replace(' ', '0');
System.out.println(s);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.