溫馨提示×

vb如何把字符串的數(shù)字截取出來

vb
小億
190
2023-08-18 16:31:04
欄目: 編程語言

可以使用正則表達式來實現(xiàn)將字符串中的數(shù)字截取出來。

以下是一個示例代碼:

Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim input As String = "abc123def456ghi789"
Dim pattern As String = "\d+" ' 匹配一個或多個數(shù)字
Dim matches As MatchCollection = Regex.Matches(input, pattern)
For Each match As Match In matches
Console.WriteLine(match.Value)
Next
End Sub
End Module

輸出結(jié)果為:

123
456
789

這里使用了Regex.Matches()方法來匹配字符串中的所有數(shù)字,并將結(jié)果存儲在MatchCollection中。然后通過遍歷MatchCollection,可以獲取每個數(shù)字的值。

0