Created
February 8, 2016 07:06
-
-
Save the-takeo/1fdfefeca318023f5da3 to your computer and use it in GitHub Desktop.
ClassExtension
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; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace ClassExtension | |
{ | |
public static class StringBuilderExtension | |
{ | |
public static StringBuilder AppendFormatLoop(this StringBuilder instance, | |
string format, IEnumerable<Object> objects) | |
{ | |
foreach (Object obj in objects) | |
{ | |
instance.AppendFormat(format, obj.ToString()); | |
} | |
return instance; | |
} | |
public static StringBuilder ForEach(this StringBuilder @this, | |
IEnumerable<object> args, string format = null, string delimiter = null) | |
{ | |
return forEach(@this, args, format, delimiter, false); | |
} | |
public static StringBuilder ForEachLine(this StringBuilder @this, | |
IEnumerable<object> args, string format = null, string delimiter = null) | |
{ | |
return forEach(@this, args, format, delimiter, true); | |
} | |
private static StringBuilder forEach(StringBuilder @this, | |
IEnumerable<object> args, string format, string delimiter, bool newline) | |
{ | |
int prev_length = @this.Length; | |
foreach (var arg in args) | |
{ | |
if (string.IsNullOrEmpty(format)) { @this.Append(arg); } | |
else { @this.AppendFormat(format, arg); } | |
if (string.IsNullOrEmpty(delimiter) == false) { @this.Append(delimiter); } | |
if (newline) { @this.AppendLine(); } | |
} | |
if (string.IsNullOrEmpty(delimiter) == false && prev_length != @this.Length) | |
{ @this.Remove(@this.Length - delimiter.Length, delimiter.Length); } | |
return @this; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment