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