Created
July 2, 2022 18:56
-
-
Save BoolPurist/b89c4cf77a767a944052010e9491a2e3 to your computer and use it in GitHub Desktop.
Extension to get string readable string representation for any sequence.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
namespace <SomeNamespace>; | |
public static class AsTextExtension | |
{ | |
public static string AsText<T>(this IEnumerable<T> sequence, string separator = ", ", int numberOfBreak = 50) | |
{ | |
var builder = new StringBuilder(); | |
builder.Append("[ "); | |
int counterForLineBreak = 0; | |
foreach (var element in sequence) | |
{ | |
counterForLineBreak++; | |
builder.Append($"{element}{separator}"); | |
if (counterForLineBreak > numberOfBreak) | |
{ | |
builder.AppendLine(); | |
counterForLineBreak = 0; | |
} | |
} | |
builder.Remove(builder.Length - separator.Length, separator.Length); | |
builder.Append(" ]"); | |
return builder.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment