检查字符串是否有字符,而不会挂起空格 [英] Check string for characters, without getting hung up on spaces
问题描述
目前,在我的程序中,我通过使用以下正则表达式
方法以及<$ c $来检查我的字符串以查看它们是否不是整数值c> if 声明:
Currently, in my program I'm checking my strings to see if they are NOT of an integer value by using the following Regex
method along with an if
statement:
if(Regex.IsMatch(myString, @"^\d+$") == false) {}
但是,我需要允许以下值:45 23 12。这些值涉及空格,我认为这些空格被 Regex
计为字符,因为每当我尝试上一个例子时,我的代码都会返回false。
However, I need to allow values such as: "45 23 12". These values involve spaces, which I believe are counted as characters by Regex
because whenever I try an example like the previous one, my code returns false.
如何在允许使用空格的情况下检查非整数值?
这个问题使用 Regex.Matches(myString,@[a-zA-Z])。计算
来检查来自'a-z'的所有字符出现的字符串, 涉及。但是,我注意到当我在 if
语句中使用它时,它的类型为Int,无法与 false $ c $进行比较c>与
==
。
This question uses Regex.Matches(myString,@"[a-zA-Z]").Count
to check the string for all character occurrences from 'a-z', which doesn't involve " ". However, I notice when I use it in my if
statement that it's of type Int, which can't be compared to false
with ==
.
推荐答案
if(! Regex.IsMatch(myString, @"^\s*\d(\d|\s)*$")) { ... }
这篇关于检查字符串是否有字符,而不会挂起空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!