using System.Diagnostics;
public static class StringExtensions {
public static int GetFirstIntFast(this string intStr)
char[] n = intStr.ToCharArray();
for (int i = 0; i < n.Length; i++)
if (n[i] >= 48 && n[i] <= 57)
if (idxFirstDigit == -1) idxFirstDigit = i;
int z = sum * 10 + (n[i] - 48);
else if (idxFirstDigit>-1)
if (intStr.IndexOf('-') == idxFirstDigit-1)
public static int GetFirstIntFaster(this string n)
for (int i = 0; i < n.Length; i++)
if (n[i] >= 48 && n[i] <= 57)
if (idxFirstDigit == -1) idxFirstDigit = i;
int z = sum * 10 + (n[i] - 48);
else if (idxFirstDigit>-1)
if (n.IndexOf('-') == idxFirstDigit-1)
public static void Main()
Stopwatch sw = new Stopwatch();
a = "aaaa12451a 1".GetFirstIntFast();
Console.WriteLine(a + " " +sw.ElapsedTicks.ToString() + " ticks");
b = "aaaa12451a 1".GetFirstIntFaster();
Console.WriteLine(b + " " +sw.ElapsedTicks.ToString() + " ticks");
a = "aaaaa-12452a 2".GetFirstIntFast();
Console.WriteLine(a + " " +sw.ElapsedTicks.ToString() + " ticks");
b = "aaaaa-12452a 2".GetFirstIntFaster();
Console.WriteLine(b + " " +sw.ElapsedTicks.ToString() + " ticks");
a = "aaaaaaccccccccccccccccccccccccccccccccc+12453a 3".GetFirstIntFast();
Console.WriteLine(a + " " +sw.ElapsedTicks.ToString() + " ticks");
b = "aaaaaaccccccccccccccccccccccccccccccccc+12453a 3".GetFirstIntFaster();
Console.WriteLine(b + " " +sw.ElapsedTicks.ToString() + " ticks");
a = "aaaaaaaaaaaaaaaaa 4".GetFirstIntFast();
Console.WriteLine(a + " " +sw.ElapsedTicks.ToString() + " ticks");
b = "aaaaaaaaaaaaaaaaa 4".GetFirstIntFaster();
Console.WriteLine(b + " " +sw.ElapsedTicks.ToString() + " ticks");
a = "1122222222222222222222 5".GetFirstIntFast();
Console.WriteLine(a + " " +sw.ElapsedTicks.ToString() + " ticks");
b = "1122222222222222222222 5".GetFirstIntFaster();
Console.WriteLine(b + " " +sw.ElapsedTicks.ToString() + " ticks");