1

I have a string that always come into this format:

"TM" + multiple Leading 0 + Number + Non-Number Character + Alphanumeric.

For example: TM000013452S20548, PB000013452S3DVSF.

How do I parse (in C# code) the varchar value to get the "Number" (13452) in this case?

2
  • iterate every character of the string to see if it is a number.. append to str.. then finally parse it. Commented Dec 6, 2019 at 21:59
  • int number = int.Parse(string.Concat(input.SkipWhile(c => !char.IsDigit(c) || c == '0').TakeWhile(char.IsDigit))); Commented Dec 6, 2019 at 22:53

3 Answers 3

2

You can use RegualarExpressions:

(?:TM|PB)0{0,}(\d+)

Like this:

string input = "For example: TM000013452S20548, PB000013452S3DVSF.";
var matches = Regex.Matches(input, @"(?:TM|PB)0{0,}(\d+)");
foreach(Match m in matches)
    Console.WriteLine(int.Parse(m.Groups[1].Value));  

Live Demo

Sign up to request clarification or add additional context in comments.

1 Comment

This wouldn't work as the OP specified that input can be PB000013452S3DVSF as well. Please make sure to fix the Regex.
1

You can use Linq:

var number = new String(
       yourString.Skip(2)
                 .SkipWhile(s => s == '0')
                 .TakeWhile(s => Char.IsDigit(s))
                 .ToArray()
          );

1 Comment

or you could replace new String with string.Concat and then you can remove the .ToArray() (not sure that makes any difference, though)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.