How to count strings and integers from an array of strings in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Friend Shared countnum As Integer = 0, countstr As Integer = 0

    Friend Shared Sub count_strings_and_integers(ByVal arr As String())
        Dim len As Integer = arr.Length

        For i As Integer = 0 To len - 1
            Try
                Dim num As Integer = Integer.Parse(arr(i))
                countnum += 1
            Catch __unusedFormatException1__ As System.FormatException
                countstr += 1
            End Try
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim arr As String() = New String() {"java", "888", "9", "c", "python", "109", "c++"}
        
		count_strings_and_integers(arr)
	
		Console.WriteLine("Numbers: " & countnum & Environment.NewLine & "Strings: " & countstr)
    End Sub
End Class



' run:
' 
' Numbers: 3
' Strings: 4
'

 



answered Feb 17, 2024 by avibootz
...