[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-08-13
3
Medium Priority
?
289 Views
Last Modified: 2012-08-13
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
Comment
Question by:mark_norge
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38287110
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
 

Author Closing Comment

by:mark_norge
ID: 38287254
Thanks a lot!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38287572
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question