我想添加一个catch语句来查看targetword是否输入了一个数字。
Console.WriteLine("Enter the word you want to guess: ")
targetWord = Console.ReadLine
答案 0 :(得分:3)
如果您想知道给定的字符串是否包含数字,您可以使用Char.IsDigit
:
Dim input = Console.ReadLine() ' for example: abc123def
Dim digits = input.Where(AddressOf Char.IsDigit)
If digits.Any() Then
' ...
End If
因此,如果您想要用户输入新字符串,直到他输入一个没有数字的字符串:
While digits.Any()
input = Console.ReadLine()
digits = input.Where(AddressOf Char.IsDigit)
End While
答案 1 :(得分:1)
使用正则表达式:
If Regex.IsMatch("tes6t", "\d") Then
Console.WriteLine("Yes")
End If
答案 2 :(得分:0)
试试这个:
''' <summary>
''' Take a regular expression test pattern, a value to test the pattern against, and return true if it matches; false otherwise.
''' </summary>
''' <param name="testPattern">The regular expression against which the <paramref name="testValue"/> will be tested.</param>
''' <param name="testValue">The value that will be tested against the regular expression <paramref name="testPattern"/>.</param>
''' <returns>True if <paramref name="testValue" /> matches the regular expression <paramref name="testPattern" />.</returns>
''' <remarks></remarks>
Private Function ValueMatchesRegExpression(ByVal testPattern As String, ByVal testValue As String) As Boolean
Dim regEx As New System.Text.RegularExpressions.Regex(testPattern)
ValueMatchesRegExpression = regEx.IsMatch(testValue)
End Function
并使用它,例如像这样:
Debug.Print(ValueMatchesRegExpression("\d", targetWord))