Find and extract a number from a string

前端 未结 29 2736
温柔的废话
温柔的废话 2020-11-22 03:19

I have a requirement to find and extract a number contained within a string.

For example, from these strings:

string test = \"1 test\"
string test1 =         


        
29条回答
  •  有刺的猬
    2020-11-22 04:00

    here is my solution

    string var = "Hello345wor705Ld";
    string alpha = string.Empty;
    string numer = string.Empty;
    foreach (char str in var)
    {
        if (char.IsDigit(str))
            numer += str.ToString();
        else
            alpha += str.ToString();
    }
    Console.WriteLine("String is: " + alpha);
    Console.WriteLine("Numeric character is: " + numer);
    Console.Read();
    

提交回复
热议问题