Repeat a piece of text
Repeat the same text a given number of times. The new string contains each copy in order.
String.Concat
Simple example
Repeat x three times.
Start'x' · 3
Resultxxx
using System;
Console.WriteLine(string.Concat(System.Linq.Enumerable.Repeat("x", 3)));
xxx
Advertisements
Everyday examples
Print four star marks on a prize ticket.
Start'*' · 4
Result****
using System;
Console.WriteLine(string.Concat(System.Linq.Enumerable.Repeat("*", 4)));
****
Draw a six-character divider for a receipt.
Start'-' · 6
Result------
using System;
Console.WriteLine(string.Concat(System.Linq.Enumerable.Repeat("-", 6)));
------
Make a three-part red-blue pattern for a ribbon label.
Start'RB' · 3
ResultRBRBRB
using System;
Console.WriteLine(string.Concat(System.Linq.Enumerable.Repeat("RB", 3)));
RBRBRB
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson