Linux 拨号vps windows公众号手机端

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

lewis 8年前 (2017-09-21) 阅读数 10 #程序编程
文章标签 vb

在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

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门