2 using System.Collections.Generic;
3 using System.Collections;
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 bool IsInteger(
this string s)
147 if (s.IsNullOrWhiteSpace())
return false;
148 return int.TryParse(s, out dummy);
158 public static bool IsNullOrEmpty(
this string s)
160 return string.IsNullOrEmpty(s);
167 public static bool IsNullOrWhiteSpace(
this string s)
169 return string.IsNullOrWhiteSpace(s);
179 public static string Build(
this string s,
object arg0)
181 if (s == null) {
return ""; }
182 return string.Format(s, arg0);
191 public static string Build(
this string s,
object arg0,
object arg1)
193 if (s == null) {
return ""; }
194 return string.Format(s, arg0, arg1);
204 public static string Build(
this string s,
object arg0,
object arg1,
object arg2)
206 if (s == null) {
return ""; }
207 return string.Format(s, arg0, arg1, arg2);
215 public static string Build(
this string s,
object[] args)
217 if (s == null) {
return ""; }
218 return string.Format(s, args);
229 public static bool IsEmail(
this string s)
231 Regex MailCheck =
new Regex(
@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
232 return MailCheck.IsMatch(s);
240 public static void WriteToFile(
this string s,
string FileName)
242 s.WriteToFile(FileName,
false);
250 public static void WriteToFile(
this string s,
string FileName,
bool Append)
252 TextWriter tw = null;
255 tw =
new StreamWriter(FileName, Append);
283 static public string Replace(
this string originalString,
string oldValue,
string newValue, StringComparison comparisonType)
285 StringBuilder sb =
new StringBuilder();
287 int previousIndex = 0;
288 int index = originalString.IndexOf(oldValue, comparisonType);
291 sb.Append(originalString.Substring(previousIndex, index - previousIndex));
293 index += oldValue.Length;
295 previousIndex = index;
296 index = originalString.IndexOf(oldValue, index, comparisonType);
298 sb.Append(originalString.Substring(previousIndex));
300 return sb.ToString();