137
1
using System;
2
using System.Diagnostics;
3
4
public static class StringExtensions {
5
6
/// <summary>
7
/// Fastest way to to find 1st number in a string and convert it into an integer
8
/// </summary>
9
/// <param name="intStr"></param>
10
/// <returns></returns>
11
//https://metadataconsulting.blogspot.com/2020/06/CSharp-Fastest-way-to-find-a-number-or-integer-in-a-string.html
12
public static int GetFirstIntFast(this string intStr)
13
{
14
15
int sum = 0; //must be zero
16
char[] n = intStr.ToCharArray(); //fastest way to index a string
17
int idxFirstDigit = -1;
18
19
for (int i = 0; i < n.Length; i++)
20
{
21
if (n[i] >= 48 && n[i] <= 57) //'0'=48 and '9'=57 get lead number only
22
{
23
if (idxFirstDigit == -1) idxFirstDigit = i;
24
int z = sum * 10 + (n[i] - 48);
Cached Result