Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove non-numeric from a string but retain decimal in C#

I am using Selenium(C#) on NUnit Framework and is getting a string from UI as $4850.19. I want to compare above string with the value from backend (DB) to assert they are equal. I am using a below method to parse my dollar amount from front-end, but the issue is that is also stripping the decimal point; and obviously the comparison with backend is failing.

Method used:

 public static string RemoveNonNumeric(string s)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
            if (Char.IsNumber(s[i]))
                sb.Append(s[i]);
        return sb.ToString();
    }

How to strip out any '$' or ',' but keep '.' in the value?

like image 926
SymboCoder Avatar asked Dec 14 '25 08:12

SymboCoder


1 Answers

With Reg ex it's trivial

Regex.Replace(s, "[^0-9.]", "")
like image 118
Vadim Alekseevsky Avatar answered Dec 15 '25 22:12

Vadim Alekseevsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!