using System.Diagnostics;
using System.Text.RegularExpressions;
public static class StringExtensions {
public static string GetAllIntRegex(this string numint)
return Regex.Replace(numint, "[^0-9]", "");
public static string GetLeadIntFast(this string numint)
if (numint.IndexOf('-') == 0)
numint = numint.Replace("-", "");
else if (numint.IndexOf('+') == 0)
numint = numint.Replace("+", "");
char[] n = numint.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n.Length; i++)
return string.Concat("-", sb.ToString());
public static string GetLeadIntFaster(this string numint)
if (numint.IndexOf('-') == 0)
numint = numint.Replace("-", "");
else if (numint.IndexOf('+') == 0)
numint = numint.Replace("+", "");
char[] n = numint.ToCharArray();
for (int i = 0; i < n.Length; i++)
if (!char.IsDigit(n[i])) {
Array.Resize(ref n, exit);
return string.Concat("-", new string(n));
public static string GetLeadIntFastest(this string numint)
if (numint.IndexOf('-') == 0)
numint = numint.Substring(1);
else if (numint.IndexOf('+') == 0)
numint = numint.Substring(1);
char[] n = numint.ToCharArray();
for (int i = 0; i < n.Length; i++)
Array.Resize(ref n, exit);
return string.Concat("-", new string(n));
public static int TryParser(this string numint)
Int32.TryParse(numint.GetLeadIntFast(), out y);
public static int TryParserFaster(this string numint)
Int32.TryParse(numint.GetLeadIntFaster(), out y);
public static int TryParserFastest(this string numint)
Int32.TryParse(numint.GetLeadIntFastest(), out y);
public static int TryParserRegex(this string numint)
Int32.TryParse(numint.GetAllIntRegex(), out y);
public static int StartsWithInt(this string numint)
if (numint.IndexOf('-') == 0)
numint = numint.Replace("-", "");
else if (numint.IndexOf('+') == 0)
numint = numint.Replace("+", "");
for (int i = 0; i < numint.Length; i++)
if (numint[i] >= '0' && numint[i] <= '9')
if (total * 10 + (numint[i] - '0') < 0)
total = total * 10 + (numint[i] - '0');
public static int StartsWithIntFaster(this string intStr)
if (intStr.IndexOf('-') == 0)
intStr = intStr.Replace("-", "");
else if (intStr.IndexOf('+') == 0)
intStr = intStr.Replace("+", "");
char[] n = intStr.ToCharArray();
for (int i = 0; i < n.Length; i++)
if (n[i] >= 48 && n[i] <= 57)
int z = sum * 10 + (n[i] - 48);
public static int StartsWithIntFastest(this string intStr)
if (intStr.IndexOf('-') == 0)
intStr = intStr.Substring(1);
else if (intStr.IndexOf('+') == 0)
intStr = intStr.Substring(1);
char[] n = intStr.ToCharArray();
for (int i = 0; i < n.Length; i++)
if (n[i] >= 48 && n[i] <= 57)
int z = sum * 10 + (n[i] - 48);
public static void Main()
Stopwatch sw = new Stopwatch();
Console.WriteLine("12451a 1");
Console.WriteLine("12451a 1".StartsWithInt());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("12452a 2");
Console.WriteLine("12452a 2".StartsWithIntFaster());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("Winner - 12453a 3");
Console.WriteLine("12453a 3".StartsWithIntFastest());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("12454a 4");
Console.WriteLine("12454a 4".TryParser());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("12455a 5");
Console.WriteLine("12455a 5".TryParserFaster());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("12456a 6");
Console.WriteLine("12456a 6".TryParserFastest());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("This grabs all numbers and NOT LEAD digits - 12457a 7");
Console.WriteLine("12457a 7".TryParserRegex());
Console.WriteLine(sw.ElapsedTicks.ToString() + " ticks");
Console.WriteLine("0".StartsWithIntFastest());
Console.WriteLine("10".StartsWithIntFastest());
Console.WriteLine("-12345".StartsWithIntFastest());
Console.WriteLine("1234".StartsWithIntFastest());
Console.WriteLine("123".StartsWithIntFastest());
Console.WriteLine("+12346".StartsWithIntFastest());
Console.WriteLine("-1234".StartsWithIntFastest());
Console.WriteLine("21474813649".StartsWithIntFastest());