patternjavaCritical
Simple way to repeat a string
Viewed 0 times
simplerepeatstringway
Problem
I'm looking for a simple commons method or operator that allows me to repeat some string n times. I know I could write this using a for loop, but I wish to avoid for loops whenever necessary and a simple direct method should exist somewhere.
Related to:
repeat string javascript
Create NSString by repeating another string a given number of times
Edited
I try to avoid for loops when they are not completely necessary because:
-
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.
String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");Related to:
repeat string javascript
Create NSString by repeating another string a given number of times
Edited
I try to avoid for loops when they are not completely necessary because:
-
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.
Solution
String::repeat". ".repeat(7) // Seven period-with-space pairs: . . . . . . .New in Java 11 is the method
String::repeat that does exactly what you asked for:String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");Its Javadoc says:
/**
* Returns a string whose value is the concatenation of this
* string repeated {@code count} times.
*
* 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
*/Code Snippets
". ".repeat(7) // Seven period-with-space pairs: . . . . . . .String str = "abc";
String repeated = str.repeat(3);
repeated.equals("abcabcabc");/**
* 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
*/Context
Stack Overflow Q#1235179, score: 422
Revisions (0)
No revisions yet.