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

Recommended Answers

All 8 Replies

To me an array is a variable. But if you look at example 1 at the link it puts them into an array. Let's say you don't want an array so you can copy each item in the array to another variable as you see fit.

commented: I'm not sure I understand. Here's what I've attempted, but it isn't working: +6

If the Daniweb posting or formatting system is too hard as to how to post code, then just paste the code in it's own reply. Someone may fix it for you but at least we'll see code.

I just figured out how to post code.

Here's what I've attempted, but it's failing:

    Dim i As Integer = 1
    Dim searchName As String
    Dim searchNo As String
    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

    For Each word As String In words
        searchNo = Convert.ToString(i)
        searchName = "Search" & searchNo

        If searchName = "search1" Then
            search1(": {0}", word)
            i += 1
        End If
        If searchName = "search2" Then
            search2(": {0}", word)
            i += 1
        End If

        etc., etc., etc.

It is giving me the error, "search1 as string - Expression is not a method."

Thanks for the code but it doesn't make sense to me. I'd use an array as noted in the first link above. With that we can iterate through the array. Or just print out the item we want.

You seem to have created this thread twice. I left something in the other thread.

Hello. The most easiest way I see to do what you ask is the following:

            Dim Search1, Search2, Search3, Search4, Search5, Search6, Search7 As String
            Dim s As String = tbxCustomerName.Text
            Dim words As String() = Split(s, " ")
            For i as Int32 = 0 To words.Length - 1
                Select Case i
                    Case 0 : Search1 = words(0)
                    Case 1 : Search2 = words(1)
                    Case 2 : Search3 = words(2)
                    Case 3 : Search4 = words(3)
                    Case 4 : Search5 = words(4)
                    Case 5 : Search6 = words(5)
                    Case 7 : Search7 = words(6)
                End Select

You're counting hooves and dividing by four. Why not just do

Dim Search As String() = s.Split()

Then instead of referencing the words as


You can just do


Plus Search is iterable.

commented: So, "strategic overcounting."! +15
Be a part of the DaniWeb community

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