Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat string/character with (format)

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"))
like image 443
automaton Avatar asked Jul 15 '14 09:07

automaton


People also ask

How do you make a string repeating characters?

Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);

How do you repeat a string in Java?

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.

How do you repeat a character in a string python?

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.

How to fill a string with repeated characters in C#?

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#?

How do you repeat a string with multiple characters in Python?

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.

How to repeat a string in Java?

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.

What is the shortest way to repeat a string?

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.


Video Answer


1 Answers

(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.

like image 69
Rainer Joswig Avatar answered Oct 09 '22 23:10

Rainer Joswig