仅检查 VB.NET 字符串中的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3374581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:58:36  来源:igfitidea点击:

Check for only digits in VB.NET String

vb.net

提问by Scott

I want to run a check on a String right before I append it to a StringBuilder to make sure only numeric characters are in the string. What's a simple way to do that?

我想在将字符串附加到 StringBuilder 之前对它进行检查,以确保字符串中只有数字字符。有什么简单的方法可以做到这一点?

回答by Gage

Use Integer.TryParse() it will return true if there are only digits in the string. Int32 max value is 2,147,483,647 so if your value is less then that then your fine. http://msdn.microsoft.com/en-us/library/f02979c7.aspx

使用 Integer.TryParse() 如果字符串中只有数字,它将返回 true。Int32 最大值是 2,147,483,647 所以如果你的值小于那么你的罚款。 http://msdn.microsoft.com/en-us/library/f02979c7.aspx

You can also use Double.TryParse() which has a max value of 1.7976931348623157E+308 but it will allow a decimal point.

您还可以使用 Double.TryParse(),它的最大值为 1.7976931348623157E+308,但它允许使用小数点。

If your looking to get the value that isnt an integer you can always go through the string one at a time

如果您想获得不是整数的值,您总是可以一次遍历一个字符串

string test = "1112003212g1232";
        int result;
        bool append=true;
        for (int i = 0; i < test.Length-1; i++)
        {
            if(!Int32.TryParse(test.Substring(i,i+1),out result))
            {
                //Not an integer
                append = false;
            }
        }

If append stays true then the string is an integer. Probably a more slick way of doing this but this should work.

如果 append 保持为真,则该字符串是一个整数。可能是一种更灵活的方法,但这应该有效。

回答by mickle sharma

Coding in VB.Net to check whether a string contains only numeric values or not.

在 VB.Net 中编码以检查字符串是否仅包含数字值。

If IsNumeric("your_text") Then
  MessageBox.Show("yes")
Else
  MessageBox.Show("no")
End If

回答by Joel Etherton

Use regular expressions:

使用正则表达式:

Dim reg as New RegEx("^\d$")

If reg.IsMatch(myStringToTest) Then
  ' Numeric
Else
  ' Not
End If

UPDATE:

更新:

You could also use linq to accomplish the same task if you're doing it in VB.Net 2008/2010.

如果您在 VB.Net 2008/2010 中执行相同的任务,您也可以使用 linq 来完成相同的任务。

Dim isNumeric as Boolean = False

Dim stringQuery = From c In myStringToTest 
                          Where Char.IsDigit(c) 
                          Select c

If stringQuery.Count <> myStringToTest.Length Then isNumeric = False

回答by Pierre-Alain Vigeant

If you do not wish to use RegEx, a simple check on each character with char.IsNumberworks.

如果您不想使用正则表达式,请对每个字符进行简单检查char.IsNumber

You can combine it with the Allextension method (in C#, I don't know how to write it in VB.net):

可以结合All扩展方法(C#中,VB.net中不知道怎么写):

string value = "78645655";
bool isValid = value.All(char.IsNumber);

Check out other charmethod, like IsDigit.

查看其他char方法,例如IsDigit.

回答by Vanmachin

2 other compact solutions :

其他 2 个紧凑型解决方案:

Without LINQ :

没有 LINQ :

Dim foo As String = "10004"
Array.Exists(foo.ToCharArray, Function(c As Char) Not Char.IsNumber(c))

With LINQ (just VB.Net equivalent of the C# version in another answer) :

使用 LINQ(在另一个答案中只是 VB.Net 相当于 C# 版本):

foo.All(Function(c As Char) Char.IsNumber(c))

回答by Andy G

Negative values haven't been mentioned, which Integer.TryParsewould accept.

没有提到负值,这是Integer.TryParse可以接受的。

I prefer UInteger.TryParsewhich will reject a negative number. However, there is an additional check required in case the value starts with "+":

我更喜欢UInteger.TryParse哪个会拒绝负数。但是,如果值以“+”开头,则需要进行额外检查:

Dim test As String = "1444"
Dim outTest As UInteger
If Not test.StartsWith("+") AndAlso UInteger.TryParse(test, outTest) Then
    MessageBox.Show("It's just digits!")
End If

or ULong.TryParsefor a larger number.

ULong.TryParse更大的数字。

回答by AllenG

Presuming you're looking at relatively short strings which will never have a number greater than the Max Int32 value, use Gage's solution. If it's a variable length and sometimes you could overflow, use Regex (System.Text.RegularExpressions)

假设您正在查看相对较短的字符串,这些字符串的数字永远不会大于 Max Int32 值,请使用 Gage 的解决方案。如果它是可变长度并且有时可能会溢出,请使用 Regex ( System.Text.RegularExpressions)

The regex for checking against just numbers is fairly routine: ^[0-9]+$

用于检查数字的正则表达式是相当常规的: ^[0-9]+$

Check herefor a very good explanation of Regex.

检查这里的正则表达式的一个很好的解释。

回答by Kyra

Pattern matching! See this, this(about.com) and this(VB.NET dev article).

模式匹配!看到这个这个(about.com)和这个(VB.NET 开发文章)。

回答by Kronass

You can use regular expression or Integer.TryParse and I prefer the regular expression check

您可以使用正则表达式或 Integer.TryParse,我更喜欢正则表达式检查