Logo

Programming-Idioms

Assign to the string s the value of the string v repeated n times, and write it out.

E.g. v="abc", n=5 ⇒ s="abcabcabcabcabc"
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
with Ada.Strings.Fixed;
use Ada.Strings.Fixed;
S : constant String := N * V;
(def s (apply str (repeat n v)))
(def s (clojure.string/join "" (repeat n v)))
#include <string>
s.reserve(v.size() * n);
for (size_t i = 0; i < n; ++i)
  s.append(v);
string s = string.Create(v.Length * n, (v, n), static (span, state) =>
{
    var (v, n) = state;
    var originSpan = v.AsSpan();
    for (var i = 0; i < n; i++)
    {
        var subSpan = span[(i * originSpan.Length)..];
        originSpan.CopyTo(subSpan);
    }
});
using System.Linq;
var s = string.Concat(Enumerable.Repeat(v, n));
using System.Text;
var sb = new StringBuilder();
for(var i = 0; i < n; i++)
	sb.Append(v);

var s = sb.ToString();
var s = v * n;
s = String.duplicate(v, n)
S = lists:concat(lists:duplicate(N, V)).
character(len=:), allocatable :: s,v
integer :: n
s = repeat(v,20)
write (*,'(A)') s
import (
	"fmt"
	"strings"
)
s := strings.Repeat(v, n)
fmt.Println(s)
s = concat $ replicate n v
const s = v.repeat(n)
import static java.util.stream.Collectors.joining;
import static java.util.stream.Stream.generate;
String s = generate(() -> v)
    .limit(n)
    .collect(joining());
for (int i=0;i<n;i++){
          s = s+v;
      }
System.out.println(s);
String s = v.repeat(n);

System.out.println(s);
val s = v.repeat(n)
println(s)
(do ((i 0 (+ i 1 )))( (= i n) s)
  (setf s (concatenate 'string s v)))
$s = str_repeat($v, $n);
echo $s;
s:= '';
for i := 1 to n do s := s + v;
writeln(s);
StrUtils
s := DupeString(v, n);
writeln(s);
my $s = $v x $n;
s = v * n
s = v * n
let s = v.repeat(n);