patterncsharpModerate
An extension to the StringBuilder
Viewed 0 times
stringbuildertheextension
Problem
This is another pretty basic class I wrote for a library as I hate the way the default
Essentially, I wanted to have the
It's pretty small and simple, so there may not be a lot to critique.
Also, before you say "just inherit
I didn't implement all the overloads of the
This can literally be used in the exact same manner as the .NET `String
StringBuilder in .NET works.Essentially, I wanted to have the
+ operator, as well as implicit conversions to strings. (Rather than needing .ToString() all the time.)It's pretty small and simple, so there may not be a lot to critique.
Also, before you say "just inherit
StringBuilder and extend it", it's sealed.///
/// This wraps the .NET StringBuilder in a slightly more easy-to-use format.
///
public class ExtendedStringBuilder
{
private StringBuilder _stringBuilder;
public string CurrentString => _stringBuilder.ToString();
public int Length => _stringBuilder.Length;
public ExtendedStringBuilder()
{
_stringBuilder = new StringBuilder();
}
public ExtendedStringBuilder(int capacity)
{
_stringBuilder = new StringBuilder(capacity);
}
public ExtendedStringBuilder Append(string s)
{
_stringBuilder.Append(s);
return this;
}
public ExtendedStringBuilder Append(char c)
{
_stringBuilder.Append(c);
return this;
}
public ExtendedStringBuilder Append(object o)
{
_stringBuilder.Append(o);
return this;
}
public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, string s) => sb.Append(s);
public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, char c) => sb.Append(c);
public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, object o) => sb.Append(o);
public static implicit operator string(ExtendedStringBuilder sb) => sb.CurrentString;
public override string ToString() => CurrentString;
public string ToString(int startIndex, int length) => _stringBuilder.ToString(startIndex, length);
}I didn't implement all the overloads of the
.Append method (yet) or the + variants of them.This can literally be used in the exact same manner as the .NET `String
Solution
A couple of quick comments:
You can use the see tag's cref attribute. If you generate documentation, some tools will generate hyperlinks for you.
The length property of a
That's a contrived example which is trivially served with
The
I'd say
I must admit, personally I think the
You can use the see tag's cref attribute. If you generate documentation, some tools will generate hyperlinks for you.
///
/// This wraps the .NET in a slightly easier to use format.
/// The length property of a
StringBuilder is read and writable. It's also really useful for it to be so:var sb = new StringBuilder();
foreach (var i in Enumerable.Range(0,10))
{
sb.AppendFormat("{0},", i);
}
sb.Length--; // removes the trailing comma.That's a contrived example which is trivially served with
string.Join but setting the length can be useful!The
_stringBuilder field should be readonly.I'd say
CurrentString is superflous. Just call _stringBuilder.ToString()I must admit, personally I think the
StringBuilder api is really good, I've never needed an implicit conversion to a string or felt the need to + rather than append to them.Code Snippets
/// <summary>
/// This wraps the .NET <see cref="StringBuilder"/> in a slightly easier to use format.
/// </summary>var sb = new StringBuilder();
foreach (var i in Enumerable.Range(0,10))
{
sb.AppendFormat("{0},", i);
}
sb.Length--; // removes the trailing comma.Context
StackExchange Code Review Q#126089, answer score: 10
Revisions (0)
No revisions yet.