阅读量:3
在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
这些示例中,假定输入字符串中只包含数字和其他字符,没有空格或其他分隔符。