Hi Group,

I'm using the split function for the very first time. I understand what it's doing. However I don't know how to output each individual word into a specific variable.

As an example, the string is "Dickerson Tile Company". I have defined 7 variables to accept up to 7 word splits.

Dim Search1, Search2, Search3 ...... Search7

Ultimately I want to output the split into

Search1 = "Dickerson"
Search2 = "Tile"
Search3 = "Company"

My code is currently

Dim s As String = tbxCustomerName.Text
Dim words As String() = s.Split(New Char() {" "c}) 'words are delimited with a space " "

This is where I'm struggling: How do I get my output into the individual buckets? My assumption is I would use an array or For statement. However I can't seem to find the correct syntax to create the name of the variable (Search1, Search2, etc.) and then to get the individual word into each variable. Can you help?

Thanks for your assistance.

Papa Don

@rprofitt,

I'm beginning to understand what you're pushing me to do. I've rewritten the code to read as:

 Dim words As String() = s.Split(New Char() {" "c})
    Dim Search1 As String
    Dim Search2 As String
    Dim Search3 As String
    Dim Search4 As String
    Dim Search5 As String
    Dim Search6 As String
    Dim Search7 As String
    Dim i As Integer = 1
    Dim searchName As Object
    For Each word As String In words
        searchName = word
        If i = 1 Then
            Search1 = searchName.ToString
        End If
        If i = 2 Then
            Search2 = searchName.ToString
        End If
        If i = 3 Then
            Search3 = searchName.ToString
        End If
        If i = 4 Then
            Search4 = searchName.ToString
        End If
        If i = 5 Then
            Search5 = searchName.ToString
        End If
        If i = 6 Then
            Search6 = searchName.ToString
        End If
        If i = 7 Then
            Search7 = searchName.ToString
        End If
        i += 1

I'm not sure that's the most efficient way to write the code. I'll continue to research the better way. But this does work. It seems I didn't understand :

         For Each word As String In words
            Console.WriteLine("WORD: {0}", word)
        Next

I discovered that "WORD: {0}" wasn't required. The sytax, "For Each word As String In words" is the actual code/loop that began to separate the words. Then it was just putting the words into the appropriate variable.

Thanks for your help and for the push. You made me think!

Since split already returns an array of strings, why would you want to use individual variables? Surely it is easier to use an array of any length rather than scalars where you will eventually have one more string than you have variables.

Split splits by default on blanks so you could just code s.split().

For future reference, never code successive If statements like that. Use Select Case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.