Is there a repeat directive for (format)
in Common lisp, something like(I know this won't work):
(format t "~5C" #\*)
Just wondering if there isn't a more elegant way to do it than this:(from rosettacode )
(defun repeat-string (n string)
(with-output-to-string (stream)
(loop repeat n do (write-string string stream))))
(princ (repeat-string 5 "hi"))
Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);
String Class repeat() Method in Java with Examplesrepeat() method is used to return String whose value is the concatenation of given String repeated count times. If the string is empty or the count is zero then the empty string is returned. Syntax: string.
In Python, we utilize the asterisk operator to repeat a string. This operator is indicated by a “*” sign. This operator iterates the string n (number) of times. The “n” is an integer value.
Here’s a small tip on using the string class better in C# to fill a string with specified repeated characters. You can use the string class constructor to fill a string that has all the repeated characters . How to Fill String with Repeated Characters in C#?
This allows us to repeat single characters, or multi-character strings: String newString = "-->" .repeat ( 5 ); assertEquals ( "-->-->-->-->-->", newString); The algorithm behind this uses loops to fill arrays of characters quite efficiently.
Save Article Like Article String Class repeat() Method in Java with Examples Last Updated :28 Jan, 2021 The string can be repeated N number of times, and we can generate a new string that has repetitions. repeat() methodis used to return String whose value is the concatenation of given String repeated count times.
941. Here is the shortest version (Java 1.5+ required): repeated = new String (new char [n]).replace ("0", s); Where n is the number of times you want to repeat the string and s is the string to repeat. No imports or libraries needed.
(defun write-repeated-string (n string stream)
(loop repeat n do (write-string string stream)))
(write-repeated-string 5 "hi" *standard-output*))
Generally you can use the format iteration:
(format t "~v@{~A~:*~}" 5 "hi")
~A
can output all kinds of items, not just characters. For more information see uselpa's linked answers.
Above takes the iteration number from the first argument. Thus the v
behind the tilde.
The rest of the arguments will be consumed by the iteration. Thus the @
.
Inside the iteration we go back one element. Thus ~:*
.
It's similar to (format t "~v{~A~:*~}" 5 '("hi"))
, which might be simpler to understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With