How to sum all even 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
            Dim n As Integer = Convert.ToInt32(mc.ToString())
            If (n Mod 2 = 0) Then 
                total += n
            End If
                
        Next
 
        Console.WriteLine(total)
 
    End Sub
 
End Module
 
 
' run:
' 
' 14

 



answered May 27, 2019 by avibootz
...