Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String interpolation - repeat [duplicate]

Is there any way to interpolate variable several times without repeating?

For example:

var name = "bla";
Console.WriteLine($"foo {name:repeat:2} bar")

to print

foo blabla bar

I'm particularly interested in interpolating several line breaks instead of repeating {Environment.NewLine} several times in the interpolation mask like this:

$"{Environment.NewLine}{Environment.NewLine}"
like image 780
Illia Ratkevych Avatar asked Jan 27 '23 04:01

Illia Ratkevych


1 Answers

public static string Repeat(this string s, int times, string separator = "")
{
    return string.Join(separator, Enumerable.Repeat(s, times));
}

Then use:

Console.WriteLine($"foo {name.Repeat(2)} bar")
like image 188
eocron Avatar answered Feb 07 '23 11:02

eocron