Stripping out non-numeric characters in string

后端 未结 11 585
南旧
南旧 2021-01-30 09:46

Hey Im looking to strip out non-numeric characters in a string in ASP.NET C#

So i.e 40,595 p.a.

would end up with 40595

Thanks

11条回答
  •  一向
    一向 (楼主)
    2021-01-30 10:23

    Here is the code using Regular Expressions:

    string str = "40,595 p.a.";
    
    StringBuilder convert = new StringBuilder();
    
    string pattern = @"\d+";
    Regex regex = new Regex(pattern);
    
    MatchCollection matches = regex.Matches(str);
    
    foreach (Match match in matches)
    {
    convert.Append(match.Groups[0].ToString());
    }
    
    int value = Convert.ToInt32(convert.ToString()); 
    

提交回复
热议问题