温馨提示×

vb怎么提取字符串中的数字

vb
小亿
335
2023-11-25 00:38:20
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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

这些示例中,假定输入字符串中只包含数字和其他字符,没有空格或其他分隔符。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:vb如何提取字符串中的数字

0