I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine
method (so I would not have to use one of the arguments for a new line character):
Imports System.Runtime.CompilerServices
Public Module sbExtension
<Extension()> _
Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
ByVal format As String, _
ByVal arg0 As Object)
oStr.AppendFormat(format, arg0).Append(ControlChars.NewLine)
End Sub
<Extension()> _
Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
ByVal format As String, ByVal arg0 As Object, _
ByVal arg1 As Object)
oStr.AppendFormat(format, arg0, arg1).Append(ControlChars.NewLine)
End Sub
<Extension()> _
Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
ByVal format As String, _
ByVal arg0 As Object, _
ByVal arg1 As Object, _
ByVal arg2 As Object)
oStr.AppendFormat(format, arg0, arg1, arg2).Append(ControlChars.NewLine)
End Sub
<Extension()> _
Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
ByVal format As String, _
ByVal ParamArray args() As Object)
oStr.AppendFormat(format, args).Append(ControlChars.NewLine)
End Sub
End Module
Here is the ported code that I came up with:
using System;
using System.Text;
public static class ExtensionLibrary
{
public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0)
{
sb.AppendFormat(format, arg0).Append(Environment.NewLine);
}
public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0, object arg1)
{
sb.AppendFormat(format, arg0, arg1).Append(Environment.NewLine);
}
public static void AppendFormattedLine(this StringBuilder sb, string format, object arg0, object arg1, object arg2)
{
sb.AppendFormat(format, arg0, arg1, arg2).Append(Environment.NewLine);
}
public static void AppendFormattedLine(this StringBuilder sb, string format, params object[] args)
{
sb.AppendFormat(format, args).Append(Environment.NewLine);
}
}
Hopefully this will come in useful for someone!
improved code, thanks joel, luke & jason.