How to extract all digits from a string in VB.NET

1 Answer

0 votes
Imports System
                  
Public Module Module1

    Public Function extract_digits(s As String) As String
        Dim digits As String = ""

        For Each ch As Char In s
            If Char.IsDigit(ch) Then
                digits &= ch
            End If
        Next

        Return digits
    End Function

    Public Sub Main()
        Dim s As String = "ab12c 9836 xy%^(s 33 * 9 100 200ppp"

        Dim digits As String = extract_digits(s)

        Console.WriteLine(digits)   ' 129836339100200
    End Sub

End Module



' run:
'
' 129836339100200
'

 



answered Dec 16, 2020 by avibootz
edited Sep 16 by avibootz
...