在VB中,您可以使用正則表達式或循環來提取字符串中的數字。以下是兩種方法的示例:
使用正則表達式:
Imports System.Text.RegularExpressions
Dim input As String = "abc123def456"
Dim regex As Regex = New Regex("\d+")
Dim matches As MatchCollection = regex.Matches(input)
For Each match As Match In matches
Console.WriteLine(match.Value)
Next
使用循環:
Dim input As String = "abc123def456"
Dim number As String = ""
For Each character As Char In input
If Char.IsDigit(character) Then
number += character
ElseIf number <> "" Then
Console.WriteLine(number)
number = ""
End If
Next
If number <> "" Then
Console.WriteLine(number)
End If
這些示例中,假定輸入字符串中只包含數字和其他字符,沒有空格或其他分隔符。