什么代码可以帮助我检查至少 2 个数字字符的字符串?

What code will help me check a string for at least 2 numericals characters?

基本上,我正在尝试使用 VB 创建一个随机密码生成器。但是我在某个地方被难住了。我不知道如何检查字符串以查看是否至少插入了 2 个数字字符。我想也许包含会有所帮助,但我只能检查一个字符。谁能帮我?目前的代码如下:

        Dim sChars As String="qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ1234567890"
        Dim sPassword As String = ""
        Dim iLength As Integer
        Do Until iLength >= 10 And sPassword.Contains(???)
        Loop

您可以使用以下代码来匹配正则表达式:

Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
    Dim regex As Regex = New Regex("\d{2}")
    Dim match As Match = regex.Match("Dot 77 Perls")
    If match.Success Then
        Console.WriteLine(match.Value)
    End If
    End Sub
End Module

这将匹配字符串中出现的两个数字和 return 结果。您可以在 web/console 应用程序中使用正则表达式匹配而不执行整个模块

您可以使用以下场景

    Dim sChars As String = "qwertyuiopasdfghjklzxcvbnmMNBV1CXZLKJHGFDSAPOIUYTREWQ"
    Dim output As String = New String((From c As Char In sChars Select c Where Char.IsDigit(c)).ToArray())
    If output.Length >= 2 Then
        MsgBox("success")
    Else
        MsgBox("Doesn't meet the requirement") ' for this input this message will displayed
    End If

假设输入是

Dim sChars As String="qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ1234567890" 则显示Success

比正则表达式方法快一点的版本:

    Dim password As String = "somePasswordToTest24hehe6"
    Dim numberCounter As Integer
    Dim counterLimit As Integer = 2
    Dim hasValidCountOfNumbers As Boolean
    For Each ch As Char In password
        If Char.IsDigit(ch) Then
           numberCounter += 1
        End If
        If numberCounter >= counterLimit Then
           hasValidCountOfNumbers = True
           Exit For
        End If
   Next

   If hasValidCountOfNumbers Then
                'do probably nothing
   Else
                'notify that password validation failed
   End If