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

Posted on 2012-08-13
Last Modified: 2012-08-13
Dear experts,

I have the following string

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,
Question by:mark_norge
    LVL 83

    Accepted Solution

    You can try following

    Dim Users As New List(of string)
    ForEach username in strNames.Split(",")
         Users.Add(username.substring(0, username.indexof("#")-1)

    Author Closing Comment

    Thanks a lot!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
            For Each UserName As String In Users

    Open in new window


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now