简体   繁体   English

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

[英]Check for only digits in VB.NET String

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. 我想在将它附加到StringBuilder之前对String进行检查,以确保字符串中只有数字字符。 What's a simple way to do that? 有什么简单的方法可以做到这一点?

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

Use Integer.TryParse() it will return true if there are only digits in the string. 使用Integer.TryParse()如果字符串中只有数字,它将返回true。 Int32 max value is 2,147,483,647 so if your value is less then that then your fine. Int32最大值是2,147,483,647,所以如果你的价值低于那么你的罚款。 http://msdn.microsoft.com/en-us/library/f02979c7.aspx 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. 如果append保持为true,则字符串为整数。 Probably a more slick way of doing this but this should work. 可能是一个更光滑的方式,但这应该工作。

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

If you do not wish to use RegEx, a simple check on each character with char.IsNumber works. 如果您不想使用RegEx,可以使用char.IsNumber对每个字符进行简单检查。

You can combine it with the All extension 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 char method, like IsDigit . 查看其他char方法,如IsDigit

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))

Negative values haven't been mentioned, which Integer.TryParse would accept. 没有提到负值, Integer.TryParse会接受。

I prefer UInteger.TryParse which will reject a negative number. 我更喜欢UInteger.TryParse ,它会拒绝负数。 However, there is an additional check required in case the value starts with "+": 但是,如果值以“+”开头,则需要进行额外检查:

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.TryParse for a larger number. ULong.TryParse为更大的数字。

Pattern matching! 模式匹配! See this , this (about.com) and this (VB.NET dev article). 看到这个这个 (about.com)和这个 (VB.NET开发文章)。

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

Presuming you're looking at relatively short strings which will never have a number greater than the Max Int32 value, use Gage's solution. 假设您正在查看相对较短的字符串,其数字永远不会超过Max Int32值,请使用Gage的解决方案。 If it's a variable length and sometimes you could overflow, use Regex ( System.Text.RegularExpressions ) 如果它是一个可变长度,有时你可能会溢出,使用正则表达式( System.Text.RegularExpressions

The regex for checking against just numbers is fairly routine: ^[0-9]+$ 用于检查数字的正则表达式是相当常规的: ^[0-9]+$

Check here for a very good explanation of Regex. 点击这里查看正则表达式的非常好的解释。

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

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