locked
I would like to extract a string, within a string, separated by commas RRS feed

  • Question

  • I have the following situation.

    Within a string, there will be 2, 3, 4 or even 5 names separated by commas.

    Example Leonard Ansin, Jim Jones, Sarah Smith, Barry Stearns

    I need to extract each name, separated by commas and place it into a variable.

    This doesn't sound to hard, but I am having problems figuring it out.

    Can you please help.

    Thank you

    Leonard


    LEONARD ANSIN

    Thursday, June 20, 2013 7:37 PM

Answers

  • Leonard,

    You can use the String.Split method and specify the comma as to split on. It would be something like:

    Dim names() As String = exampleString.Split(","c)

    That string array will now contain the values between the commas.


    Please call me Frank :)

    • Proposed as answer by IronRazerz Friday, June 21, 2013 12:14 AM
    • Marked as answer by Youen Zen Monday, July 1, 2013 6:43 AM
    Thursday, June 20, 2013 7:41 PM
  • Dim input = "Leonard Ansin, Jim Jones, Sarah Smith, Barry Stearns"
    
    Dim names As String() = input.Split(","C).ToArray()
    For Each name  In names
    	Console.WriteLine(name)
    Next


    • Edited by Venkat786 Thursday, June 20, 2013 7:42 PM typo
    • Proposed as answer by IronRazerz Friday, June 21, 2013 12:15 AM
    • Marked as answer by Youen Zen Monday, July 1, 2013 6:42 AM
    Thursday, June 20, 2013 7:41 PM
  • There should be a lot of examples of this in the forums already.

    You will want to use the Microsoft.VisualBasic.FileIO.TextFieldParser to read the lines of the file into an array of strings.  Then you can add those arrays to a list or use them to populate a data table or store them in some other way.

    Please check the sample in the documentation and search these forums for TextFieldParser.  Feel free to post back with your code if you run into a specific problem, or if there is something in the documentation and related threads that you don't understand.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Youen Zen Monday, July 1, 2013 6:42 AM
    Thursday, June 20, 2013 7:42 PM
  • Hello,

    The following may very well be more than you want. The variable MyNames is our string to work with which I noticed there are spaces after each comma so to make the split work properly Replace method is used. PeopleList creates a List(Of Person) where Person class allows if needed to work with the data i.e. locate, sort, add, edit remove in a English format.

    Class

    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
        Public Sub New()
        End Sub
        Public Overrides Function ToString() As String
            Return FirstName & " " & LastName
        End Function
    End Class

    Example for obtaining data. Please note I change the order of names to demo sorting the data.

    Dim MyNames As String = "Barry Stearns, Jim Jones, Sarah Smith, Leonard Ansin"
    Dim PeopleList =
        (
            From T In MyNames.Replace(", ", ",").Split(","c)
            Let Item = T.TrimStart.Split(" "c)
            Select New Person With
                   {
                       .FirstName = Item(0),
                       .LastName = Item(1)
                   }
            ).OrderBy(
            Function(p) p.LastName
        ).ToList
    For Each p In PeopleList
        Console.WriteLine("[{0}] ", p.ToString)
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Youen Zen Monday, July 1, 2013 6:43 AM
    Thursday, June 20, 2013 9:33 PM

All replies

  • You can use String.Split to do this.

    For example, in your case, try:

    Dim names = original.Split(New String() {", "}, StringSplitOptions.None)

    This splits by comma followed by space (so you don't get a leading space before the second and subsequent names).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 20, 2013 7:40 PM
  • Leonard,

    You can use the String.Split method and specify the comma as to split on. It would be something like:

    Dim names() As String = exampleString.Split(","c)

    That string array will now contain the values between the commas.


    Please call me Frank :)

    • Proposed as answer by IronRazerz Friday, June 21, 2013 12:14 AM
    • Marked as answer by Youen Zen Monday, July 1, 2013 6:43 AM
    Thursday, June 20, 2013 7:41 PM
  • Dim input = "Leonard Ansin, Jim Jones, Sarah Smith, Barry Stearns"
    
    Dim names As String() = input.Split(","C).ToArray()
    For Each name  In names
    	Console.WriteLine(name)
    Next


    • Edited by Venkat786 Thursday, June 20, 2013 7:42 PM typo
    • Proposed as answer by IronRazerz Friday, June 21, 2013 12:15 AM
    • Marked as answer by Youen Zen Monday, July 1, 2013 6:42 AM
    Thursday, June 20, 2013 7:41 PM
  • There should be a lot of examples of this in the forums already.

    You will want to use the Microsoft.VisualBasic.FileIO.TextFieldParser to read the lines of the file into an array of strings.  Then you can add those arrays to a list or use them to populate a data table or store them in some other way.

    Please check the sample in the documentation and search these forums for TextFieldParser.  Feel free to post back with your code if you run into a specific problem, or if there is something in the documentation and related threads that you don't understand.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Youen Zen Monday, July 1, 2013 6:42 AM
    Thursday, June 20, 2013 7:42 PM
  • Thank you

    This was great.

    Please keep this question open for a few days, until I extract data from the table.


    LEONARD ANSIN

    Thursday, June 20, 2013 8:43 PM
  • Hello,

    The following may very well be more than you want. The variable MyNames is our string to work with which I noticed there are spaces after each comma so to make the split work properly Replace method is used. PeopleList creates a List(Of Person) where Person class allows if needed to work with the data i.e. locate, sort, add, edit remove in a English format.

    Class

    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
        Public Sub New()
        End Sub
        Public Overrides Function ToString() As String
            Return FirstName & " " & LastName
        End Function
    End Class

    Example for obtaining data. Please note I change the order of names to demo sorting the data.

    Dim MyNames As String = "Barry Stearns, Jim Jones, Sarah Smith, Leonard Ansin"
    Dim PeopleList =
        (
            From T In MyNames.Replace(", ", ",").Split(","c)
            Let Item = T.TrimStart.Split(" "c)
            Select New Person With
                   {
                       .FirstName = Item(0),
                       .LastName = Item(1)
                   }
            ).OrderBy(
            Function(p) p.LastName
        ).ToList
    For Each p In PeopleList
        Console.WriteLine("[{0}] ", p.ToString)
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    • Marked as answer by Youen Zen Monday, July 1, 2013 6:43 AM
    Thursday, June 20, 2013 9:33 PM
  • Thank you for your efforts in assisting me.

    The problem has now been solved.

    Best regards'

    Leonard


    LEONARD ANSIN

    Monday, July 1, 2013 6:40 PM