简体   繁体   English

从每个十进制数中删除字符串中的几个点/逗号

[英]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.现在用户想要输入并添加一个逗号,然后将是“x1+5,54”,这不再是一个数字了。 How can I check this with an if?我怎样才能用if来检查这个?

Something like if the last number already contains a comma don't append another one.如果最后一个数字已经包含逗号,则不要 append 另一个数字。

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.当用户输入额外的逗号时,您会得到错误,因此您可以处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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