可以使用正则表达式来实现将字符串中的数字截取出来。
以下是一个示例代码:
Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim input As String = "abc123def456ghi789" Dim pattern As String = "\d+" ' 匹配一个或多个数字 Dim matches As MatchCollection = Regex.Matches(input, pattern) For Each match As Match In matches Console.WriteLine(match.Value) Next End Sub End Module
输出结果为:
123 456 789
这里使用了Regex.Matches()
方法来匹配字符串中的所有数字,并将结果存储在MatchCollection
中。然后通过遍历MatchCollection
,可以获取每个数字的值。