我正在寻找一个简单的公共方法或操作符,允许我重复一些字符串n次。我知道我可以使用for循环来写这个,但我希望在必要时避免for循环,并且应该在某个地方存在一个简单的直接方法。
String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");
相关:
重复字符串javascript
通过重复给定次数的另一个字符串创建NSString
编辑
当它们不是完全必要的时候,我尽量避免使用for循环,因为:
They add to the number of lines of code even if they are tucked away in another function.
Someone reading my code has to figure out what I am doing in that for loop. Even if it is commented and has meaningful variables names, they still have to make sure it is not doing anything "clever".
Programmers love to put clever things in for loops, even if I write it to "only do what it is intended to do", that does not preclude someone coming along and adding some additional clever "fix".
They are very often easy to get wrong. For loops involving indexes tend to generate off by one bugs.
For loops often reuse the same variables, increasing the chance of really hard to find scoping bugs.
For loops increase the number of places a bug hunter has to look.
所以你想避免循环?
现在你知道了:
public static String repeat(String s, int times) {
if (times <= 0) return "";
else return s + repeat(s, times-1);
}
(当然我知道这是丑陋和低效的,但它没有循环:-p)
你想要更简单更漂亮吗?使用jython:
s * 3
编辑:让我们优化一下:-D
public static String repeat(String s, int times) {
if (times <= 0) return "";
else if (times % 2 == 0) return repeat(s+s, times/2);
else return s + repeat(s+s, times/2);
}
Edit2:我已经为4个主要的替代方案做了一个快速而粗略的基准测试,但我没有时间多次运行它来获得几个输入的平均值和绘制时间……如果有人想试试,下面是代码:
public class Repeat {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
String s = args[1];
int l = s.length();
long start, end;
start = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
if(repeatLog2(s,i).length()!=i*l) throw new RuntimeException();
}
end = System.currentTimeMillis();
System.out.println("RecLog2Concat: " + (end-start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
if(repeatR(s,i).length()!=i*l) throw new RuntimeException();
}
end = System.currentTimeMillis();
System.out.println("RecLinConcat: " + (end-start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
if(repeatIc(s,i).length()!=i*l) throw new RuntimeException();
}
end = System.currentTimeMillis();
System.out.println("IterConcat: " + (end-start) + "ms");
start = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
if(repeatSb(s,i).length()!=i*l) throw new RuntimeException();
}
end = System.currentTimeMillis();
System.out.println("IterStrB: " + (end-start) + "ms");
}
public static String repeatLog2(String s, int times) {
if (times <= 0) {
return "";
}
else if (times % 2 == 0) {
return repeatLog2(s+s, times/2);
}
else {
return s + repeatLog2(s+s, times/2);
}
}
public static String repeatR(String s, int times) {
if (times <= 0) {
return "";
}
else {
return s + repeatR(s, times-1);
}
}
public static String repeatIc(String s, int times) {
String tmp = "";
for (int i = 0; i < times; i++) {
tmp += s;
}
return tmp;
}
public static String repeatSb(String s, int n) {
final StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(s);
}
return sb.toString();
}
}
它有两个参数,第一个是迭代的次数(每个函数运行的重复次数参数从1..n),第二个是要重复的字符串。
到目前为止,快速检查不同输入的运行时间,得出的排名大致如下(从好到坏):
迭代StringBuilder追加(1x)。
递归连接log2调用(~3x)。
递归级联线性调用(~30x)。
迭代级联线性(~45x)。
我从来没有想到递归函数比for循环快:-o
玩得开心。
如果你只知道输出字符串的长度(它可能不能被输入字符串的长度整除),那么使用这个方法:
static String repeat(String s, int length) {
return s.length() >= length ? s.substring(0, length) : repeat(s + s, length);
}
使用演示:
for (int i = 0; i < 50; i++)
System.out.println(repeat("_/‾\\", i));
不要使用空s,长度为> 0,因为在这种情况下不可能得到想要的结果。
我想要一个函数来为JDBC创建一个以逗号分隔的问号列表,然后找到了这篇文章。所以,我决定采用两个变体,看看哪个表现更好。在100万次迭代之后,普通的StringBuilder花了2秒(fun1),而神秘的最优版本(fun2)花了30秒。又变得神秘有什么意义?
private static String fun1(int size) {
StringBuilder sb = new StringBuilder(size * 2);
for (int i = 0; i < size; i++) {
sb.append(",?");
}
return sb.substring(1);
}
private static String fun2(int size) {
return new String(new char[size]).replaceAll("\0", ",?").substring(1);
}
如果你使用的是Java <= 7,这是最“简洁”的:
// create a string made up of n copies of string s
String.format("%0" + n + "d", 0).replace("0", s);
在Java 8及以上版本中,有一种更易读的方式:
// create a string made up of n copies of string s
String.join("", Collections.nCopies(n, s));
最后,对于Java 11及以上版本,有一个专门用于此目的的新的repeat (int count)方法(链接)
"abc".repeat(12);
或者,如果您的项目使用java库,则有更多的选项。
对于Apache Commons:
StringUtils.repeat("abc", 12);
谷歌番石榴:
Strings.repeat("abc", 12);