Jypeli  5
The simple game programming library
StringHelpers.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 
6 namespace Jypeli
7 {
8  public static class StringHelpers
9  {
10 #if XBOX
11  public static void Clear( this StringBuilder sb )
12  {
13  sb.Length = 0;
14  }
15 #endif
16 
23  public static bool StringEquals(object o1, object o2)
24  {
25  if ( o1 == o2 ) return true;
26 
27  string s1 = null;
28  string s2 = null;
29 
30  if ( o1 is string ) s1 = (string)o1;
31  else if (o1 is StringBuilder) s1 = ((StringBuilder)o1).ToString();
32  else return false;
33 
34  if ( o2 is string ) s2 = (string)o2;
35  else if (o2 is StringBuilder) s2 = ((StringBuilder)o2).ToString();
36  else return false;
37 
38  return s1.Equals(s2);
39  }
40 
48  public static StringBuilder RemoveLast(this StringBuilder builder, int chars)
49  {
50  if ( builder.Length <= chars )
51  {
52  builder.Length = 0;
53  return builder;
54  }
55 
56  builder.Length -= chars;
57  return builder;
58  }
59 
67  public static void Substring( this StringBuilder src, StringBuilder dest, int start, int length )
68  {
69  for ( int i = start; i < length; i++ )
70  {
71  dest.Append(src[i]);
72  }
73  }
74 
82  public static StringBuilder Substring( this StringBuilder builder, int start, int length )
83  {
84  StringBuilder result = new StringBuilder();
85  builder.Substring( start, length );
86  return result;
87  }
88 
94  public static void PutTo( this StringBuilder src, StringBuilder dest )
95  {
96  dest.Append( src );
97  src.Clear();
98  }
99 
107  public static void PutTo( this StringBuilder src, StringBuilder dest, int start, int length )
108  {
109  src.Substring( dest, start, length );
110  src.Remove( start, length );
111  }
112 
120  public static StringBuilder PutTo( this StringBuilder builder, int start, int length )
121  {
122  StringBuilder removed = builder.Substring( start, length );
123  builder.Remove( start, length );
124  return removed;
125  }
126 
127  public static int IndexForWhich( this StringBuilder builder, Predicate<char> pred )
128  {
129  for ( int i = 0; i < builder.Length; i++ )
130  {
131  if ( pred( builder[i] ) )
132  return i;
133  }
134 
135  return -1;
136  }
137 
138  public static int IndexOf( this StringBuilder builder, char c )
139  {
140  return builder.IndexForWhich( ch => ch == c );
141  }
142 
143  public static void RemoveLeading( this StringBuilder builder, Predicate<char> pred )
144  {
145  int i = 0;
146  for ( i = 0; i < builder.Length; i++ )
147  {
148  if ( !pred( builder[i] ) )
149  break;
150  }
151 
152  if (i > 0)
153  builder.Remove( 0, i );
154  }
155 
156  public static void RemoveTrailing( this StringBuilder builder, Predicate<char> pred )
157  {
158  int i = 0;
159  for ( i = builder.Length - 1; i >= 0; i-- )
160  {
161  if ( !pred( builder[i] ) )
162  break;
163  }
164 
165  if (i < builder.Length - 1)
166  builder.Remove( i + 1, builder.Length - i - 1 );
167  }
168 
169  public static void Trim( this StringBuilder builder )
170  {
171  builder.RemoveLeading( c => Char.IsWhiteSpace( c ) );
172  builder.RemoveTrailing( c => Char.IsWhiteSpace( c ) );
173  }
174 
175  public static void TakeFirstWord( this StringBuilder src, StringBuilder dest )
176  {
177  int ws = src.IndexForWhich( c => char.IsWhiteSpace( c ) );
178  if ( ws < 0 ) src.PutTo( dest );
179  else src.PutTo( dest, 0, ws + 1 );
180  }
181 
188  public static string Repeat( this string s, int times )
189  {
190  StringBuilder sb = new StringBuilder();
191 
192  for (int i = 0; i < times; i++)
193  sb.Append( s );
194 
195  return sb.ToString();
196  }
197 
198  public static string[] Append( this string s, params object[] ends )
199  {
200  string[] results = new string[ends.Length];
201 
202  for ( int i = 0; i < ends.Length; i++ )
203  {
204  results[i] = s + ends[i];
205  }
206 
207  return results;
208  }
209  }
210 }
static void Trim(this StringBuilder builder)
static string [] Append(this string s, params object[] ends)
static StringBuilder PutTo(this StringBuilder builder, int start, int length)
Poistaa osamerkkijonon ja palauttaa sen toisena StringBuilderina..
static void RemoveLeading(this StringBuilder builder, Predicate< char > pred)
static void RemoveTrailing(this StringBuilder builder, Predicate< char > pred)
static StringBuilder Substring(this StringBuilder builder, int start, int length)
Palauttaa osamerkkijonon toisena StringBuilderina.
static void Substring(this StringBuilder src, StringBuilder dest, int start, int length)
Kirjoittaa osamerkkijonon toiseen StringBuilderiin.
static bool StringEquals(object o1, object o2)
Vertaa kahta oliota, jotka ovat joko merkkijonoja tai StringBuildereita, merkki kerrallaan.
static StringBuilder RemoveLast(this StringBuilder builder, int chars)
Poistaa merkkijonon lopusta tietyn määrän merkkejä. Jos merkkijono on lyhyempi kuin poistettava määrä...
static int IndexForWhich(this StringBuilder builder, Predicate< char > pred)
static int IndexOf(this StringBuilder builder, char c)
static void PutTo(this StringBuilder src, StringBuilder dest)
Poistaa koko StringBuilderin sisällön ja kirjoittaa sen toiseen StringBuilderiin. ...
static void TakeFirstWord(this StringBuilder src, StringBuilder dest)
static void PutTo(this StringBuilder src, StringBuilder dest, int start, int length)
Poistaa osamerkkijonon ja kirjoittaa sen toiseen StringBuilderiin.
static string Repeat(this string s, int times)
Toistaa merkkijonon annetun määrän verran.