• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

How to split a string on 2 chars, but only store one of them in a string array

Dear experts,

I have the following string
username1#date1,username2#date2,username3#date3

I would like to split the string on , and #, but only store the usernames in a string array. How can I do this best.

Thanks for any help,
MB
0
mark_norge
Asked:
mark_norge
1 Solution
 
CodeCruiserCommented:
You can try following

Dim Users As New List(of string)
ForEach username in strNames.Split(",")
     Users.Add(username.substring(0, username.indexof("#")-1)
Next
0
 
mark_norgeAuthor Commented:
Thanks a lot!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also Split() using both characters at the onset, and then only use the even numbered items:
        Dim Users As New List(Of String)
        Dim data As String = "username1#date1,username2#date2,username3#date3"

        Dim values() As String = data.Split("#,".ToCharArray) ' <-- split on both # and ,
        For i As Integer = 0 To values.GetUpperBound(0) Step 2
            Users.Add(values(i))
        Next

        For Each UserName As String In Users
            Debug.Print(UserName)
        Next

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now