Advertisement
maujogador

ExtendedStringBuilder

Apr 21st, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. public class ExtendedStringBuilder
  2. {
  3.     private StringBuilder _stringBuilder;
  4.  
  5.     public string CurrentString
  6.     {
  7.         get { return _stringBuilder.ToString(); }
  8.     }
  9.  
  10.     public int Length
  11.     {
  12.         get { return _stringBuilder.Length; }
  13.     }
  14.  
  15.     public ExtendedStringBuilder()
  16.     {
  17.         _stringBuilder = new StringBuilder();
  18.     }
  19.  
  20.     public ExtendedStringBuilder(int capacity)
  21.     {
  22.         _stringBuilder = new StringBuilder(capacity);
  23.     }
  24.  
  25.     public ExtendedStringBuilder Append(object o)
  26.     {
  27.         if (o is char)
  28.             _stringBuilder.Append((char)o);
  29.         else if ((o as string) != null)
  30.             _stringBuilder.Append((string)o);
  31.         else
  32.             _stringBuilder.Append(o);
  33.         return this;
  34.     }
  35.  
  36.     public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, object o)
  37.     {
  38.         if (o is char)
  39.             return sb.Append((char)o);
  40.         if ((o as string) != null)
  41.             sb.Append((string)o);
  42.         return sb.Append(o);
  43.     }
  44.  
  45.     public static implicit operator string(ExtendedStringBuilder sb)
  46.     {
  47.         return sb.CurrentString;
  48.     }
  49.  
  50.     public static implicit operator ExtendedStringBuilder(string s)
  51.     {
  52.         return new ExtendedStringBuilder().Append(s);
  53.     }
  54.  
  55.     public override string ToString()
  56.     {
  57.         return CurrentString;
  58.     }
  59.  
  60.     public string ToString(int startIndex, int length)
  61.     {
  62.         return _stringBuilder.ToString(startIndex, length);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement