Find and extract a number from a string

前端 未结 29 2800
温柔的废话
温柔的废话 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 03:43

    var match=Regex.Match(@"a99b",@"\d+");
    if(match.Success)
    {
        int val;
        if(int.TryParse(match.Value,out val))
        {
            //val is set
        }
    }
    

提交回复
热议问题