简体   繁体   中英

Remove several dots/commas in string from every decimal number

How can I check for a comma in a number?

Lets assume I have a string which represents a polynomial term that looks like this

string x = "x+1+5,54";

Now the user wants to put in and add a comma which will then be "x1+5,54," which is not a number anymore. How can I check this with an if?

Something like if the last number already contains a comma don't append another one.

Use regular expression.

        if (Regex.IsMatch(a, @"^((\d+,?\d*)|(\w?))([-+/]((\d+,?\d*)|(\w?)))*$"))
        {
            //correct
        }
        else
        {
            //incorrect
        }

You'll get false, when user inputs extra comma, so you can handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM