How to use Substring method to separate key value pairs from array of strings in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Sub Main()
        Dim keyval As String() = {"Name=Arthur", "Age=157", "Enducation=Hogwarts", "Location=Scotland"}

        For Each pair In keyval
            Dim position As Integer = pair.IndexOf("=")

            If position < 0 Then
                Continue For
            End If

            Console.WriteLine("Key:{0} - Value:{1}", pair.Substring(0, position), pair.Substring(position + 1))
        Next
    End Sub
End Class



' run:
'
' Key:Name - Value:Arthur
' Key:Age - Value:157
' Key:Enducation - Value:Hogwarts
' Key:Location - Value:Scotland
'

 



answered Aug 8, 2023 by avibootz
...