Skip to content

Instantly share code, notes, and snippets.

@tufanbarisyildirim
Created October 13, 2011 23:44
Show Gist options
  • Save tufanbarisyildirim/1285853 to your computer and use it in GitHub Desktop.
Save tufanbarisyildirim/1285853 to your computer and use it in GitHub Desktop.
CSharp Extensions
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Globalization;
namespace Extensions
{
public static class StringExtensions
{
private const string UrlPattern = "(ht|f)tps?:\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?";
public static bool IsNumeric(this string str)
{
return Regex.IsMatch(str, "^[0-9]+$");
}
public static bool IsMatch(this string str, string pattern)
{
return Regex.IsMatch(str, pattern);
}
public static bool IsUrl(this string str)
{
return Regex.IsMatch(str, String.Format("^{0}$", UrlPattern), RegexOptions.IgnoreCase);
}
public static bool ContainsUrl(this string str)
{
return Regex.IsMatch(str, UrlPattern, RegexOptions.IgnoreCase);
}
public static Regex ToRegex(this string pattern)
{
return new Regex(pattern);
}
public static Regex ToRegex(this string pattern, RegexOptions options)
{
return new Regex(pattern, options);
}
public static int ToInt32(this string str)
{
return Convert.ToInt32(str);
}
public static long ToInt64(this string str)
{
return Convert.ToInt64(str);
}
public static string DeleteNumerics(this string str)
{
return Regex.Replace(str, "[0-9]", "");
}
public static string DeleteNonNumerics(this string str)
{
return Regex.Replace(str, "[^0-9]", "");
}
/// <summary>
/// Original Source From : http://master.web.tr/tc-kimlik-no-dogrulama-function-v2.aspx
/// </summary>
/// <returns></returns>
public static bool IsTcKimlik(this string tcKimlikNo)
{
bool returnvalue = false;
if (tcKimlikNo.Length == 11 && tcKimlikNo.IsNumeric())
{
Int64 ATCNO, BTCNO, TcNo;
long C1, C2, C3, C4, C5, C6, C7, C8, C9, Q1, Q2;
TcNo = Int64.Parse(tcKimlikNo);
ATCNO = TcNo / 100;
BTCNO = TcNo / 100;
C1 = ATCNO % 10; ATCNO = ATCNO / 10;
C2 = ATCNO % 10; ATCNO = ATCNO / 10;
C3 = ATCNO % 10; ATCNO = ATCNO / 10;
C4 = ATCNO % 10; ATCNO = ATCNO / 10;
C5 = ATCNO % 10; ATCNO = ATCNO / 10;
C6 = ATCNO % 10; ATCNO = ATCNO / 10;
C7 = ATCNO % 10; ATCNO = ATCNO / 10;
C8 = ATCNO % 10; ATCNO = ATCNO / 10;
C9 = ATCNO % 10; ATCNO = ATCNO / 10;
Q1 = ((10 - ((((C1 + C3 + C5 + C7 + C9) * 3) + (C2 + C4 + C6 + C8)) % 10)) % 10);
Q2 = ((10 - (((((C2 + C4 + C6 + C8) + Q1) * 3) + (C1 + C3 + C5 + C7 + C9)) % 10)) % 10);
returnvalue = ((BTCNO * 100) + (Q1 * 10) + Q2 == TcNo);
}
return returnvalue;
}
// c# implementation of php.net/str_split
public static string[] SplitStr(this string str, int count)
{
if (str.Length <= count)
return new string[] { str };
List<string> list = new List<string>();
string son = "";
foreach (char harf in str.ToCharArray())
{
if (son.Length < count)
son += harf;
else
{
list.Add(son);
son = harf.ToString();
}
}
if (son.Length > 0)
list.Add(son);
return list.ToArray();
}
public static char ToCharFromHexValue(this string str)
{
return (char)str.HexToInt();
}
public static int HexToInt(this string str)
{
return str.HexToInt32();
}
public static int HexToInt32(this string str)
{
if (!str.IsMatch("[0-9A-F]+"))
throw new Exception(str + " isn't a hex value");
return Convert.ToInt32(str, 16);
}
public static int HexToInt64(this string str)
{
if (!str.IsMatch("[0-9A-F]+"))
throw new Exception(str + " isn't a hex value");
return Convert.ToInt32(str, 16);
}
public static string HexToBinary(this string hexvalue)
{
return Convert.ToString(Convert.ToInt64(hexvalue, 16), 2);
}
/// <summary>
/// source: http://www.testingreflections.com/node/view/5635s
/// </summary>
/// <param name="asciiString"></param>
/// <returns></returns>
public static string ConvertToHex(this string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
public static char[] ToCharArrayFromHexString(this string str, bool filternull = true)
{
List<char> chars = new List<char>();
foreach (string s in str.SplitStr(2))
{
if (!filternull || (s != "00" && s != "0"))
chars.Add(s.ToCharFromHexValue());
}
return chars.ToArray();
}
public static string HexConvert(this string str)
{
return String.Join("", str.ToCharArrayFromHexString());
}
public static string ToMd5(this string text)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(text));
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (byte b in bytes)
{
builder.Append(b.ToString("x2").ToLower());
}
return builder.ToString();
}
}
}
@kemalbalaban
Copy link

good work thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment