public static class StringExtension
{
private static readonly Regex WebUrlExpression = new Regex(@”(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?”, RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex EmailExpression = new Regex(@”^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$”, RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex StripHTMLExpression = new Regex(“<\\S[^><]*>”, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);
private static readonly Regex AlphaNumericExpression = new Regex(@”^[a-zA-Z0-9]*$”, RegexOptions.Singleline | RegexOptions.Compiled);

private static readonly char[] IllegalUrlCharacters = new[] { ‘;’, ‘/’, ‘\\’, ‘?’, ‘:’, ‘@’, ‘&’, ‘=’, ‘+’, ‘$’, ‘,’, ‘<‘, ‘>’, ‘#’, ‘%’, ‘.’, ‘!’, ‘*’, ‘\”, ‘”‘, ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘, ‘}’, ‘|’, ‘^’, ‘`’, ‘~’, ‘–’, ‘‘’, ‘’’, ‘“’, ‘”’, ‘»’, ‘«’ };

[DebuggerStepThrough]
public static bool IsAlphaNumeric(this string target)
{
return !string.IsNullOrEmpty(target) && AlphaNumericExpression.IsMatch(target);
}

[DebuggerStepThrough]
public static bool IsPhoneNumber(this string target)
{
return !string.IsNullOrEmpty(target.Trim()) && target.Replace(” “, “”).ToCharArray().All(x => char.IsDigit(x) || x == ‘-‘ || x == ‘(‘ || x == ‘)’);
}

[DebuggerStepThrough]
public static bool IsWebUrl(this string target)
{
return !string.IsNullOrEmpty(target) && WebUrlExpression.IsMatch(target);
}

[DebuggerStepThrough]
public static bool IsEmail(this string target)
{
return !string.IsNullOrEmpty(target) && EmailExpression.IsMatch(target);
}

[DebuggerStepThrough]
public static bool IsNullOrEmpty(this string target)
{
return string.IsNullOrEmpty(target);
}

[DebuggerStepThrough]
public static bool IsNotNullOrEmpty(this string target)
{
return !string.IsNullOrEmpty(target);
}

[DebuggerStepThrough]
public static string NullSafe(this string target)
{
return (target ?? string.Empty).Trim();
}

[DebuggerStepThrough]
public static string StripHtml(this string target)
{
return StripHTMLExpression.Replace(target, string.Empty);
}

[DebuggerStepThrough]
public static Guid ToGuid(this string target)
{
Guid result = Guid.Empty;

if ((!string.IsNullOrEmpty(target)) && (target.Trim().Length == 22))
{
string encoded = string.Concat(target.Trim().Replace(“-“, “+”).Replace(“_”, “/”), “==”);

try
{
byte[] base64 = Convert.FromBase64String(encoded);

result = new Guid(base64);
}
catch (FormatException)
{
}
}

return result;
}

[DebuggerStepThrough]
public static string ToLegalUrl(this string target)
{
if (string.IsNullOrEmpty(target))
{
return target;
}

target = target.Trim();

if (target.IndexOfAny(IllegalUrlCharacters) > -1)
{
foreach (char character in IllegalUrlCharacters)
{
target = target.Replace(character.ToString(CultureInfo.CurrentCulture), string.Empty);
}
}

target = target.Replace(” “, “-“);

while (target.Contains(“–“))
{
target = target.Replace(“–“, “-“);
}

return target;
}

[DebuggerStepThrough]
public static string UrlEncode(this string target)
{
return HttpUtility.UrlEncode(target);
}

[DebuggerStepThrough]
public static string UrlDecode(this string target)
{
return HttpUtility.UrlDecode(target);
}

[DebuggerStepThrough]
public static string AttributeEncode(this string target)
{
return HttpUtility.HtmlAttributeEncode(target);
}

[DebuggerStepThrough]
public static string HtmlEncode(this string target)
{
return HttpUtility.HtmlEncode(target);
}

[DebuggerStepThrough]
public static string HtmlDecode(this string target)
{
return HttpUtility.HtmlDecode(target);
}

public static string Replace(this string target, ICollection<string> oldValues, string newValue)
{
oldValues.ForEach(oldValue => target = target.Replace(oldValue, newValue));
return target;
}

public static string SafeSubString(this string target, int startIndex, int length)
{
return startIndex + length > target.Length ? target.Substring(startIndex, target.Length) : target.Substring(startIndex, length);
}
}

Advertisements