生成 n 个重复字符的字符串的最简单方法是什么?
给定一个字符 c 和一个数字 n,如何创建一个由 n 个重复的 c 组成的字符串?手动执行太麻烦了:
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; ++i)
{
sb.append(c);
}
String result = sb.toString();
肯定有一些静态库函数已经为我执行此操作了吗?
Given a character c and a number n, how can I create a String that consists of n repetitions of c? Doing it manually is too cumbersome:
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; ++i)
{
sb.append(c);
}
String result = sb.toString();
Surely there is some static library function that already does this for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
编辑:
这个答案已经提交九年了,但它仍然时不时地引起一些关注。与此同时,Java 8 引入了函数式编程功能。给定一个 char
c
和所需的重复次数count
,下面的单行代码可以执行与上面相同的操作。但请注意,它比数组方法慢。除了最苛刻的情况外,这几乎不重要。除非它是在每秒执行数千次的某些代码中,否则不会有太大区别。这也可以与字符串而不是字符一起使用来重复多次,因此更加灵活。不需要第三方库。
EDIT:
It's been 9 years since this answer was submitted but it still attracts some attention now and then. In the meantime Java 8 has been introduced with functional programming features. Given a char
c
and the desired number of repetitionscount
the following one-liner can do the same as above.Do note however that it is slower than the array approach. It should hardly matter in any but the most demanding circumstances. Unless it's in some piece of code that will be executed thousands of times per second it won't make much difference. This can also be used with a String instead of a char to repeat it a number of times so it's a bit more flexible. No third-party libraries needed.
如果可以,请使用 StringUtils 来自 Apache Commons Lang:
If you can, use StringUtils from Apache Commons Lang:
谷歌番石榴时间!
http://docs.guava-libraries.googlecode.com/ git/javadoc/com/google/common/base/Strings.html
Google Guava Time!
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html
为了了解速度损失,我测试了两个版本,一个使用 Array.fill,另一个使用 StringBuilder。
并
使用
(注意main函数中的循环是为了启动JIT)
结果如下:
这是一个巨大差异
To have an idea of the speed penalty, I have tested two versions, one with Array.fill and one with StringBuilder.
and
using
(note the loop in main function is to kick in JIT)
The results are as follows:
It is a huge difference
Java SE 11
String#repeat (int count)
作为 Java SE 11 的一部分引入,使其变得非常容易实现。演示:
输出:
Java SE 11
String#repeat(int count)
, introduced as part of Java SE 11, makes it quite easy to do.Demo:
Output:
看看这个示例
答案,它是在java中重复字符串的简单方法,所以在那里投票
take look at this sample
answer taken form Simple way to repeat a String in java so vote up there
这是一个基于标准二进制供电算法的 O(logN) 方法:
reps
的负值返回空字符串。Here is an O(logN) method, based on the standard binary powering algorithm:
Negative values for
reps
return the empty string.只需将其添加到您自己的...
或者 Apache commons 有一个可以添加的实用程序类。
Just add it to your own...
Or Apache commons has a utility class you can add.
我添加另一种方法来解决它:
I am adding another way to solve it:
如果您不是使用 Java 11+(否则我会选择 Arvind Kumar Avinash 的答案),您也可以结合
String#join(CharSequence, Iterable
和Collections#nCopies( int,T)
:If you are not on Java 11+ (otherwise I would go with Arvind Kumar Avinash's answer), you can also combine
String#join(CharSequence, Iterable<? extends CharSequence>
andCollections#nCopies(int, T)
:一个简单而干净的解决方案是使用 String.format,然后用所需的字符替换字符。
如果您使用的是 Java 11,
则只需使用 String.repeat()
A simple and clean solution would be to use String.format and then replace characters with desired character.
If you are using Java 11
you can just use String.repeat()