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

1 Answer

0 votes
Imports System.Text.RegularExpressions
 
Module Module1
 
    Sub Main()
 
        Dim s As String = "1 VB.NET 13 Java 89 Python 2 C 4"
        Dim total As Integer = 0
 
        Dim mcollection As MatchCollection = Regex.Matches(s, "\d")
 
        For Each mc As Match In mcollection
            total += Convert.ToInt32(mc.ToString())
        Next
 
        Console.WriteLine(total)
 
    End Sub
 
End Module
 
 
' run:
' 
' 28

 



answered May 27, 2019 by avibootz
...