2 using System.Collections.Generic;
6 using System.Text.RegularExpressions;
15 public static class StringExtensions
21 public static int HexToInt(
this string s)
23 return int.Parse(s,
System.Globalization.NumberStyles.HexNumber);
29 public static int HexToByte(
this string s)
31 return byte.Parse(s,
System.Globalization.NumberStyles.HexNumber);
37 public static bool IsHexString(
this string s)
39 return s.IsHexString(0, s.Length);
49 public static bool IsHexString(
this string s,
int startindex)
51 return s.IsHexString(startindex, s.Length - startindex);
62 public static bool IsHexString(
this string s,
int startindex,
int length)
64 if (
string.IsNullOrWhiteSpace(s))
return false;
66 if (startindex + length > s.Length)
return false;
68 return System.Text.RegularExpressions.Regex.IsMatch(s.Substring(startindex, length),
@"\A\b[0-9a-fA-F]+\b\Z");
78 public static byte[] ToByteArray(
this string s)
80 System.Text.UTF8Encoding encoding =
new System.Text.UTF8Encoding();
81 return encoding.GetBytes(s);
88 public static StringBuilder ToStringBuilder(
this string s)
90 if (
string.IsNullOrEmpty(s))
92 return new StringBuilder(
"");
94 return new StringBuilder(s);
102 public static string Left(
this string s,
int length)
104 return s.Substring(0, length);
112 public static string Right(
this string s,
int length)
114 return s.Substring(s.Length - length, length);
124 public static string Mid(
this string s,
int startIndex,
int length)
126 return s.Substring(startIndex, length);
133 public static int ToInteger(
this string s)
135 int integerValue = 0;
136 int.TryParse(s, out integerValue);
144 public static uint ToUInt(
this string s)
146 uint uintegerValue = 0;
147 uint.TryParse(s, out uintegerValue);
148 return uintegerValue;
155 public static bool IsInteger(
this string s)
158 if (s.IsNullOrWhiteSpace())
return false;
159 return int.TryParse(s, out dummy);
166 public static bool IsUInt(
this string s)
169 if (s.IsNullOrWhiteSpace())
return false;
170 return uint.TryParse(s, out dummy);
178 public static bool IsNullOrEmpty(
this string s)
180 return string.IsNullOrEmpty(s);
187 public static bool IsNullOrWhiteSpace(
this string s)
189 return string.IsNullOrWhiteSpace(s);
199 public static string Build(
this string s,
object arg0)
201 if (s == null) {
return ""; }
202 return string.Format(s, arg0);
211 public static string Build(
this string s,
object arg0,
object arg1)
213 if (s == null) {
return ""; }
214 return string.Format(s, arg0, arg1);
224 public static string Build(
this string s,
object arg0,
object arg1,
object arg2)
226 if (s == null) {
return ""; }
227 return string.Format(s, arg0, arg1, arg2);
235 public static string Build(
this string s,
object[] args)
237 if (s == null) {
return ""; }
238 return string.Format(s, args);
249 public static bool IsEmail(
this string s)
251 Regex MailCheck =
new Regex(
@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
252 return MailCheck.IsMatch(s);
260 public static void WriteToFile(
this string s,
string FileName)
262 s.WriteToFile(FileName,
false);
270 public static void WriteToFile(
this string s,
string FileName,
bool Append)
272 TextWriter tw = null;
275 tw =
new StreamWriter(FileName, Append);
303 static public string Replace(
this string originalString,
string oldValue,
string newValue, StringComparison comparisonType)
305 StringBuilder sb =
new StringBuilder();
307 int previousIndex = 0;
308 int index = originalString.IndexOf(oldValue, comparisonType);
311 sb.Append(originalString.Substring(previousIndex, index - previousIndex));
313 index += oldValue.Length;
315 previousIndex = index;
316 index = originalString.IndexOf(oldValue, index, comparisonType);
318 sb.Append(originalString.Substring(previousIndex));
320 return sb.ToString();
328 static public byte[] GetBytes(
this string str)
330 byte[] bytes =
new byte[str.Length *
sizeof(char)];
331 System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
Animation steps from left to right through the source image