Simple way to repeat a String in java
我正在寻找一个简单的通用方法或运算符,它允许我重复一些字符串n次。我知道我可以使用for循环编写这个函数,但是我希望在必要时避免for循环,并且应该在某个地方存在一个简单的直接方法。
相关:
重复字符串javascript通过重复给定次数的另一个字符串来创建nsstring
编辑
我尽量避免不完全必要的循环,因为:
它们会增加代码行数,即使它们被塞进另一个函数中。
阅读我的代码的人必须弄清楚我在for循环中做了什么。即使它被注释并且有意义的变量名,它们仍然必须确保它没有做任何"聪明"的事情。
程序员喜欢把聪明的东西放在循环中,即使我把它写成"只做它打算做的事情",这并不排除有人会加入一些聪明的"修复"。
它们往往很容易出错。对于包含索引的循环,往往由一个错误生成。
因为循环经常重复使用相同的变量,增加了很难找到范围缺陷的机会。
对于循环,增加bug猎人必须查看的地方的数量。
这里是最短版(Java 1.5 +要求):
1 |
在一个时代
不需要进口或库。
长stringutils.repeat(共用)
用法:
如果你使用的是Java<=7,这是一个简单的,因为它得到:
1 2 |
在Java中有5 8及以上单:
1 2 |
Java的新方法,通过增加一个
1 2 | int n = 3; "abc".repeat(n); |
在Java的
1 2 |
从Java在线11,有
它的Javadoc说:P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Returns a string whose value is the concatenation of this * string repeated {@code count} times. * <p> * If this string is empty or count is zero then the empty * string is returned. * * @param count number of times to repeat * * @return A string composed of this string repeated * {@code count} times or the empty string if this * string is empty or count is zero * * @throws IllegalArgumentException if the {@code count} is * negative. * * @since 11 */ |
这是一个标准的方式来做它仅用字符串函数和没有明确的循环:
1 2 |
如果你像我想使用谷歌的番石榴和Apache Commons and not。你可以用the repeat the method在番石榴的字符串类。P></
1 | Strings.repeat("-", 60); |
用java
1 2 3 | import static java.util.stream.Collectors.joining; ... String repeated = Stream.generate(() ->"abc").limit(3).collect(joining()); //"abcabcabc" |
你可以在"工具/方法:简单的if neededP></
1 2 3 |
所以你想避免环路?
在这里你有它:
1 2 3 4 |
(当然,我知道这是一个丑陋的和低效的,但它没有循环:P)
你想和它的简单prettier?使用Jython。
1 | s * 3 |
编辑:让我们命名它一点:D
1 2 3 4 5 |
我所做的edit2:快速和肮脏的4种基准为主,但我没有时间做它的几个时代得到的均值和输入几个时代的情节……所以在这里的代码,如果任何人想要试试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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(); } } |
它需要2个参数,第一个数(每个功能重复迭代运行时报与精氨酸和1 ..n)是第二个字符串重复。
到目前为止,《纽约时报》的快速检测和投入运行不同的排序是这样的(叶更好。):
我不会有什么guessed recursive函数更快,比
有乐趣(功能的XD)。
这不比你的问题包含的字符
1 2 3 4 5 6 7 8 9 10 |
基于Fortran语言的答案,这是一个使用StringBuilder recusive版本:A
1 2 3 4 5 6 7 8 9 10 11 | public static void repeat(StringBuilder stringBuilder, String s, int times) { if (times > 0) { repeat(stringBuilder.append(s), s, times - 1); } } public static String repeat(String s, int times) { StringBuilder stringBuilder = new StringBuilder(s.length() * times); repeat(stringBuilder, s, times); return stringBuilder.toString(); } |
一个简单的表征:使用美元。
1 2 3 4 5 | @Test public void repeatString() { String string ="abc"; assertThat($(string).repeat(3).toString(), is("abcabcabc")); } |
PS:所以在阵列重复的作品集,列表,等等
想要的函数创建的列表项的问题- delimited Marks of this for JDBC的用途,和发现。我决定把两variants,see which to酮和更好的完成。在iterations茶园1万,2 -(fun1 StringBuilder的品种了,and the seconds)supposedly黑莓cryptic最优版本(fun2 30 seconds)了。什么是cryptic of the点了吗?P></
1 2 3 4 5 6 7 8 9 10 11 |
面向对象的解决方案
在回答每个近似分枝函数作为静电解决方案,面向对象的思维(but for reusability -用途和清晰度)想出解决方案路径代表一charsequence -接口(which also the opens UPS在线usability mutable charsequence -类)。P></
the following can be used with or或者类没有separator串/ charsequence and each呼叫"toString()builds the最终重复灌水"字符串。are not only the输入/ separator串级公司to which can be,but every(例如charsequence StringBuilder类implements,StringBuffer,等)。P></源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | /** * Helper-Class for Repeating Strings and other CharSequence-Implementations * @author Maciej Schuttkowski */ public class RepeatingCharSequence implements CharSequence { final int count; CharSequence internalCharSeq =""; CharSequence separator =""; /** * CONSTRUCTOR - RepeatingCharSequence * @param input CharSequence to repeat * @param count Repeat-Count */ public RepeatingCharSequence(CharSequence input, int count) { if(count < 0) throw new IllegalArgumentException("Can not repeat String ""+input+"" less than 0 times! count="+count); if(count > 0) internalCharSeq = input; this.count = count; } /** * CONSTRUCTOR - Strings.RepeatingCharSequence * @param input CharSequence to repeat * @param count Repeat-Count * @param separator Separator-Sequence to use */ public RepeatingCharSequence(CharSequence input, int count, CharSequence separator) { this(input, count); this.separator = separator; } @Override public CharSequence subSequence(int start, int end) { checkBounds(start); checkBounds(end); int subLen = end - start; if (subLen < 0) { throw new IndexOutOfBoundsException("Illegal subSequence-Length:"+subLen); } return (start == 0 && end == length()) ? this : toString().substring(start, subLen); } @Override public int length() { //We return the total length of our CharSequences with the separator 1 time less than amount of repeats: return count < 1 ? 0 : ( (internalCharSeq.length()*count) + (separator.length()*(count-1))); } @Override public char charAt(int index) { final int internalIndex = internalIndex(index); //Delegate to Separator-CharSequence or Input-CharSequence depending on internal index: if(internalIndex > internalCharSeq.length()-1) { return separator.charAt(internalIndex-internalCharSeq.length()); } return internalCharSeq.charAt(internalIndex); } @Override public String toString() { return count < 1 ?"" : new StringBuilder(this).toString(); } private void checkBounds(int index) { if(index < 0 || index >= length()) throw new IndexOutOfBoundsException("Index out of Bounds:"+index); } private int internalIndex(int index) { // We need to add 1 Separator-Length to total length before dividing, // as we subtracted one Separator-Length in"length()" return index % ((length()+separator.length())/count); } } |
用途:——
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public static void main(String[] args) { //String input ="12345"; //StringBuffer input = new StringBuffer("12345"); StringBuilder input = new StringBuilder("123"); //String separator ="<=>"; StringBuilder separator = new StringBuilder("<=");//.append('>'); int repeatCount = 2; CharSequence repSeq = new RepeatingCharSequence(input, repeatCount, separator); String repStr = repSeq.toString(); System.out.println("Repeat="+repeatCount+"\tSeparator="+separator+"\tInput="+input+"\tLength="+input.length()); System.out.println("CharSeq:\tLength="+repSeq.length()+"\tVal="+repSeq); System.out.println("String :\tLength="+repStr.length()+"\tVal="+repStr); //Here comes the Magic with a StringBuilder as Input, as you can append to the String-Builder //and at the same Time your Repeating-Sequence's toString()-Method returns the updated String :) input.append("ff"); System.out.println(repSeq); //Same can be done with the Separator: separator.append("===").append('>'); System.out.println(repSeq); } |
输出:example
1 2 3 4 5 | Repeat=2 Separator=<= Input=123 Length=3 CharSeq: Length=8 Val=123<=123 String : Length=8 Val=123<=123 123ff<=123ff 123ff<====>123ff |
仅用JRE班(从指定源)和试图减少临时对象的数目,你可以写什么样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static String repeat(String toRepeat, int times) { if (toRepeat == null) { toRepeat =""; } if (times < 0) { times = 0; } final int length = toRepeat.length(); final int total = length * times; final char[] src = toRepeat.toCharArray(); char[] dst = new char[total]; for (int i = 0; i < total; i += length) { System.arraycopy(src, 0, dst, i, length); } return String.copyValueOf(dst); } |
编辑
你可以尝试在没有回路:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
编辑2
即使使用较短的词是:
1 2 3 4 5 6 7 | public static String repeat3(String toRepeat, int times) { return Collections.nCopies(times, toRepeat). toString(). replace("[",""). replace("]",""). replaceAll(",",""); } |
然而我仍然像第一版本。
readability of和for the portability:清酒P></
1 2 3 4 |
如果速度是你的关注,那么你应该使用less as as possible Copying记忆。thus en is of chars与arrays来工作。P></
1 2 3 4 5 6 7 8 |
速度的测试,使用最优方法相似:stirngbuilder is like thisP></
1 2 3 4 5 6 |
测试代码:EN and the toP></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static void main(String... args) { String res; long time; for (int j = 0; j < 1000; j++) { res = repeatString("123", 100000); res = repeatStringSB("123", 100000); } time = System.nanoTime(); res = repeatString("123", 1000000); time = System.nanoTime() - time; System.out.println("elapsed repeatString:" + time); time = System.nanoTime(); res = repeatStringSB("123", 1000000); time = System.nanoTime() - time; System.out.println("elapsed repeatStringSB:" + time); } |
我从这里运行结果和系统:theP></
1 2 | elapsed repeatString: 6006571 elapsed repeatStringSB: 9064937 |
that the is for循环测试已知的最优结果在JIT和踢我。P></
最短not the,but the Way(我认为)fastest is to the:使用StringBuilderP></
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Repeat a String as many times you need. * * @param i - Number of Repeating the String. * @param s - The String wich you want repeated. * @return The string n - times. */ public static String repeate(int i, String s) { StringBuilder sb = new StringBuilder(); for (int j = 0; j < i; j++) sb.append(s); return sb.toString(); } |
如果你是担心性能使用StringBuilder,只是在做.tostring()A环上的环境。写你自己的尾巴,它的使用和重用的类。最大5线的代码。
单回路P></
1 2 3 4 5 6 7 |
本尝试超时:P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static char[] myABCs = {'a', 'b', 'c'}; public static int numInput; static Scanner in = new Scanner(System.in); public static void main(String[] args) { System.out.print("Enter Number of Times to repeat:"); numInput = in.nextInt(); repeatArray(numInput); } public static int repeatArray(int y) { for (int a = 0; a < y; a++) { for (int b = 0; b < myABCs.length; b++) { System.out.print(myABCs[b]); } System.out.print(""); } return y; } |
使用递归,You can do the following(使用一个在线ternary代理商,最大值):P></
1 2 3 |
我知道,这是丑陋和probably not one but是高效,在线!P></
我真的享受这一问题。there is of很多的知识和风格。我不能让它在我的摇滚节目);P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | { String string = repeat("1234567890", 4); System.out.println(string); System.out.println("======="); repeatWithoutCopySample(string, 100000); System.out.println(string);// This take time, try it without printing System.out.println(string.length()); } /** * The core of the task. */ @SuppressWarnings("AssignmentToMethodParameter") public static char[] repeat(char[] sample, int times) { char[] r = new char[sample.length * times]; while (--times > -1) { System.arraycopy(sample, 0, r, times * sample.length, sample.length); } return r; } /** * Java classic style. */ public static String repeat(String sample, int times) { return new String(repeat(sample.toCharArray(), times)); } /** * Java extreme memory style. */ @SuppressWarnings("UseSpecificCatch") public static void repeatWithoutCopySample(String sample, int times) { try { Field valueStringField = String.class.getDeclaredField("value"); valueStringField.setAccessible(true); valueStringField.set(sample, repeat((char[]) valueStringField.get(sample), times)); } catch (Exception ex) { throw new RuntimeException(ex); } } |
你喜欢它吗?P></
1 2 3 4 5 6 7 8 9 |
你的欲望和需要使用循环,我认为你应该使用一个回路。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
你需要使用一个for循环的原因,一是不好的。在回应你的批评:
stringutils.java stringutils.java here is the latestP></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public static String repeat(String str, int repeat) { // Performance tuned for 2.0 (JDK1.4) if (str == null) { return null; } if (repeat <= 0) { return EMPTY; } int inputLength = str.length(); if (repeat == 1 || inputLength == 0) { return str; } if (inputLength == 1 && repeat <= PAD_LIMIT) { return repeat(str.charAt(0), repeat); } int outputLength = inputLength * repeat; switch (inputLength) { case 1 : return repeat(str.charAt(0), repeat); case 2 : char ch0 = str.charAt(0); char ch1 = str.charAt(1); char[] output2 = new char[outputLength]; for (int i = repeat * 2 - 2; i >= 0; i--, i--) { output2[i] = ch0; output2[i + 1] = ch1; } return new String(output2); default : StringBuilder buf = new StringBuilder(outputLength); for (int i = 0; i < repeat; i++) { buf.append(str); } return buf.toString(); } } |
它甚至不need to be this can be into this大,自制,and can be copied和糊在你进入公用事业类项目。P></
1 2 3 4 5 6 7 8 |
我想知道E5,the best to this Way to do the会好的只是使用上述代码,或any of the answers here。但如果是只是太大朗共用的小项目P></
在递归法的created the same thing that给你想要的。感到自由使用this…P></
1 2 3 |
i have the same在线答案可以在Java字符串to repeat序列乘积?P></
1 | repeated = str + str + str; |
有时简单的是最好的。每个人都能看到《代码阅读发生了什么。P></
*编译器会做的东西