If all the fields are fixed width, and all you care about is the first integer, then it's pretty easy; just use string.Substring to extract the part you care about and then parse it.
Here's how to do the extract and parse (note that I use int.TryParse - you are parsing a possibly corrupted string):
private bool TryExtractFirstNumber(string input, out int result)
{
var resultString = input.Substring(2, 9);
return int.TryParse(resultString, out result);
}
You can call this like:
var inputs = new[]
{
"TM000013452S20548",
"PB000013452S3DVSF",
};
foreach (var inp in inputs)
{
if (TryExtractFirstNumber(inp, out var result))
{
Debug.WriteLine(result);
}
}
The output from that is:
13452
13452
If the position of the "Non-Number Character" that you describe is not known, go looking for it:
private int FindIndexOfFirstNonNumeric(string toScan, int startIndex = 0)
{
for (var index = startIndex; index < toScan.Length; ++index)
{
if (!char.IsNumber(toScan[index]))
{
return index;
}
}
return toScan.Length;
}
and then modify the TryExtractFirstNumber function to look for it:
private bool TryExtractFirstNumber(string input, out int result)
{
var length = FindIndexOfFirstNonNumeric(input, 2) - 2;
var resultString = input.Substring(2, length);
return int.TryParse(resultString, out result);
}
It gives the same results.
int number = int.Parse(string.Concat(input.SkipWhile(c => !char.IsDigit(c) || c == '0').TakeWhile(char.IsDigit)));