9 public static void Clear(
this StringBuilder sb )
23 if ( o1 == o2 )
return true;
28 if ( o1 is
string ) s1 = (string)o1;
29 else if (o1 is StringBuilder) s1 = ((StringBuilder)o1).ToString();
32 if ( o2 is
string ) s2 = (string)o2;
33 else if (o2 is StringBuilder) s2 = ((StringBuilder)o2).ToString();
46 public static StringBuilder
RemoveLast(
this StringBuilder builder,
int chars)
48 if ( builder.Length <= chars )
54 builder.Length -= chars;
65 public static void Substring(
this StringBuilder src, StringBuilder dest,
int start,
int length )
67 for (
int i = start; i < length; i++ )
80 public static StringBuilder
Substring(
this StringBuilder builder,
int start,
int length )
82 StringBuilder result =
new StringBuilder();
83 builder.Substring( start, length );
92 public static void PutTo(
this StringBuilder src, StringBuilder dest )
105 public static void PutTo(
this StringBuilder src, StringBuilder dest,
int start,
int length )
107 src.Substring( dest, start, length );
108 src.Remove( start, length );
118 public static StringBuilder
PutTo(
this StringBuilder builder,
int start,
int length )
120 StringBuilder removed = builder.Substring( start, length );
121 builder.Remove( start, length );
125 public static int IndexForWhich(
this StringBuilder builder, Predicate<char> pred )
127 for (
int i = 0; i < builder.Length; i++ )
129 if ( pred( builder[i] ) )
136 public static int IndexOf(
this StringBuilder builder,
char c )
138 return builder.IndexForWhich( ch => ch == c );
141 public static void RemoveLeading(
this StringBuilder builder, Predicate<char> pred )
144 for ( i = 0; i < builder.Length; i++ )
146 if ( !pred( builder[i] ) )
151 builder.Remove( 0, i );
154 public static void RemoveTrailing(
this StringBuilder builder, Predicate<char> pred )
157 for ( i = builder.Length - 1; i >= 0; i-- )
159 if ( !pred( builder[i] ) )
163 if (i < builder.Length - 1)
164 builder.Remove( i + 1, builder.Length - i - 1 );
167 public static void Trim(
this StringBuilder builder )
169 builder.RemoveLeading( c => Char.IsWhiteSpace( c ) );
170 builder.RemoveTrailing( c => Char.IsWhiteSpace( c ) );
173 public static void TakeFirstWord(
this StringBuilder src, StringBuilder dest )
175 int ws = src.IndexForWhich( c =>
char.IsWhiteSpace( c ) );
176 if ( ws < 0 ) src.PutTo( dest );
177 else src.PutTo( dest, 0, ws + 1 );
186 public static string Repeat(
this string s,
int times )
188 StringBuilder sb =
new StringBuilder();
190 for (
int i = 0; i < times; i++)
193 return sb.ToString();
196 public static string[]
Append(
this string s, params
object[] ends )
198 string[] results =
new string[ends.Length];
200 for (
int i = 0; i < ends.Length; i++ )
202 results[i] = s + ends[i];