Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Split Comma Separated String and Store it in to Variables VB Net

I have a comma separated string like this

Dim str as String = "1,5"
Dim num1, num2 As Integer 

What I want is I want to separate the string through Comma and store these values to the new integer variables. After separation I want something like this

num1 = 1
num2 = 5

I want to do something like this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 num1, num2 = Convert.ToInt32(str.Split(",")) 

this is possible in Python but I don’t know how to do it here

>Solution :

You can split your string and assign into variables like this:

Private Sub AssignVars()

    Dim str As String = "1,5"
    Dim num1, num2 As Integer
    Dim results() As String

    results = str.Split(Convert.ToChar(","))

    For pos As Integer = 0 To results.Count - 1
        Select Case pos
            Case 0
                Integer.TryParse(results(pos), num1)
            Case 1
                Integer.TryParse(results(pos), num2)
            Case Else
                'error handle?
        End Select
    Next


End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading