typescript单行条件语句不工作
我得到一个数字作为字符串。现在我想检查字符串值是否为整型。所以我写了这行代码:typescript单行条件语句不工作,typescript,Typescript,我得到一个数字作为字符串。现在我想检查字符串值是否为整型。所以我写了这行代码: const isDotOrComma = this.strValue.indexOf('.') || this.strValue.indexOf(','); if (isDotOrComma === -1 ) { intType = this.strValue; } 因此,通过上面的代码,我试图检查这些字符串“123,34”或“123.34”,“12334”,无论它们是否为整数 不幸的是,上面的代码正在检查这
const isDotOrComma = this.strValue.indexOf('.') || this.strValue.indexOf(',');
if (isDotOrComma === -1 ) {
intType = this.strValue;
}
因此,通过上面的代码,我试图检查这些字符串“123,34”或“123.34”,“12334”
,无论它们是否为整数
不幸的是,上面的代码正在检查这些点。如果是逗号,它就不起作用了。我做错了什么?如何解决此问题?有没有更好的办法
可能是上面的代码正在使用JavaScript。但是typescript失败。运算符将返回第一个未定义的值,在本例中为-1,因为您首先检查点。我将使用
includes
方法,而不是indexOf
方法
const isDotOrComma = this.strValue.includes('.') || this.strValue.includes(',');
if (!isDotOrComma) {
intType = this.strValue;
}
谢谢你如此明智的回答。接受并向上投票。因为我要存储整数。if语句应与'if!(逗号)`。因此,如果数字有逗号点,那么它不是整数。请编辑您的答案@robert koss好吗