0

I wanted to insert multiple new lines in a string so I used an interpolated string as my choice of a format mechanism which should be pretty fast.

Example:

string mystring = $"one line{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}another line{Environment.NewLine}end";

Now that works but how can I better insert multiple Environment.Newline in an interpolated string; for example if I wanted 15 this gets cumbersome.

I am aware of string.Replace(, string.Format(), and concatenation Environment.NewLine + Environment.NewLine + Environment.NewLine as well as literal string @"stuff" with blank lines however is not the question (an alternative) but rather how to insert multiples such my fake example of

string mystring = $"one line{Environment.NewLine(3)}another line{Environment.NewLine}end text";

Mark Schultheiss
  • 28,892
  • 9
  • 63
  • 88

3 Answers3

2

You could write a simple helper method to get a repeated string, and then call that:

static string Repeat(string input, int count)
{
    var result = new StringBuilder();
    for (var i = 0; i < count; i++) result.Append(input);
    return result.ToString();
}

string mystring = 
    $"one line{Repeat(Environment.NewLine, 3)}another line{Environment.NewLine}end";

And if you don't want to explicitly call another function, you could write an extension method:

public static class Extensions
{
    public static string Repeat(this string input, int count)
    {
        if (input == null) return null;
        if (count < 1) return string.Empty;
        var result = new StringBuilder();
        for (var i = 0; i < count; i++) result.Append(input);
        return result.ToString();
    }
}

And then the syntax is simpler:

string mystring = 
    $"one line{Environment.NewLine.Repeat(3)}another line{Environment.NewLine}end";
Rufus L
  • 32,853
  • 5
  • 25
  • 38
  • Noted in question I know about this option, thanks – Mark Schultheiss Jan 07 '19 at 19:24
  • Updated answer. – Rufus L Jan 07 '19 at 19:27
  • One reason is to optimize for a highly performance driven application thus creating a `StringBuilder` or doing a loop, calling a custom method etc. is/was not an option considered. Thanks for posting however as it may fit someone else's circumstance. – Mark Schultheiss Jan 07 '19 at 19:29
  • 1
    I'm pretty sure that any solution that takes a string and a count is going to use a loop behind the scenes. I.e. [`Enumerable.Repeat`](https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/Repeat.cs), and [`StringBuilder.Insert`](https://referencesource.microsoft.com/#mscorlib/system/text/stringbuilder.cs,4fc7620ab6118309). For performance, I guess you want something that resolves at compile time. Not sure about that one. – Rufus L Jan 07 '19 at 19:40
2

Linq can do this:

var fifteenNewlines = string.Concat(Enumerable.Repeat(Environment.NewLine, 15));
Dour High Arch
  • 20,700
  • 27
  • 72
  • 83
1

Maybe not the most elegant solution, but you can call a method from within an interpolated string. The method can return a specified number of new lines.

private string NewLines(int lines)
{           
 return new StringBuilder().Insert(0,$"{Environment.NewLine}, lines").ToString();
}

Then just call

string mystring = $"one line{NewLines(3)}another line{NewLines(1)}end text";