Results 1 to 17 of 17

Thread: [RESOLVED] Splitting string with multiple delimiters

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] Splitting string with multiple delimiters

    I can't see where I am going wrong. I have a text file with this in it:

    dept=div1,div2,div3
    dept2=div1,div4

    I tried this but where does the string after the = go?

    Code:
     Dim tfLines() As String = System.IO.File.ReadAllLines("text.ini") ' File to load.
            For Each line As String In tfLines ' Load and read all lines in file.
                DeptDropDown = line.Split("="c) ' Split using =.
                DivDropDown = DeptDropDown(1).Split(","c) ' Split using ,.
               
            Next
    That errors out on the second split with : "Index was outside the bounds of the array."

    My idea was to load dropdown1 and once changed load what is after the = sign to another drop down depending on what was selected in dropdown1. So if I selected "dept" in dropdown1 then dropdown2 will contain div1 div2 div3 in succession

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Splitting string with multiple delimiters

    DivDropDown = line.split(new string(){"=", ","}, stringsplitoptions.none)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Splitting string with multiple delimiters

    Actually, that wouldn't work. I'd recommend regex.split
    Can you post a more specific example of how each line would be?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: Splitting string with multiple delimiters

    The best thing to do is to set up a breakpoint on both and see what strings are being returned, but in this particular case I'd do something like this:
    Code:
        Sub Main()
            Dim txtfile() As String = {"dept=div1,div2,div3", "dept2=div1,div4"}
    
            For Each line As String In txtfile
                'Get the department by getting the substring from the beginning to the first instance of the equal sign
                Dim dept As String = line.Substring(0, line.IndexOf("="c))
                'Get all divs that are after the equal sign and seperated by commas
                Dim divs() As String = line.Substring(line.IndexOf("="c) + 1).Split(","c)
    
                'Write out what department we found
                Console.WriteLine("Department: " & dept & " has the following DIV's")
                For Each div As String In divs
                    'Write out each div found
                    Console.WriteLine("    " & div)
                Next
    
                'Write a seperating line to let the user know we started a new department
                Console.WriteLine("------------------")
            Next
    
            Console.ReadLine()
    
        End Sub
    Edit - I see that .paul. beat me to the punch :P
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Splitting string with multiple delimiters

    ok. i'm at my PC now...

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sedisplaynder As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.DataSource = datasource(IO.File.ReadAllLines("C:\Users\Paul\Desktop\text.ini")).ToArray
        End Sub
    
        Private Iterator Function datasource(arr() As String) As IEnumerable(Of listItem)
            For Each line As String In arr
                If line.Trim = "" Then Continue For
                Dim fields() As String = line.Split(New String() {"=", ","}, StringSplitOptions.None)
                Yield New listItem With {.display = fields(0), .value = fields.Skip(1).ToArray}
            Next
        End Function
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            ComboBox2.DataSource = DirectCast(ComboBox1.SelectedItem, listItem).value
        End Sub
    
    End Class
    
    Public Class listItem
        Public display As String
        Public value() As String
        Public Overrides Function ToString() As String
            Return Me.display
        End Function
    End Class

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Splitting string with multiple delimiters

    Thank you guys. dday9, I couldn't quite get yours to do what I wanted, gave me errors on the first line.substring, but I believe it was more my code stopping it then anything else. But paul, thank you very much, I added it as a module because I will have a lot of forms using the same values in other dropdowns, no sense of using the same code over and over in different spots

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Splitting string with multiple delimiters

    small problem paul, when I save the form it errors out saying "Conversion from type "listitem" to type String is not valid"

    Saving it like this

    row("PODept") = Replace(CStr(CmboPODept.SelectedItem), "'", "`")

    and it doesn't like that line

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Splitting string with multiple delimiters

    change:

    Code:
    CStr(CmboPODept.SelectedItem)
    to:

    Code:
    CmboPODept.GetItemText(CmboPODept.SelectedItem)

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Splitting string with multiple delimiters

    or just:

    Code:
    CmboPODept.Text
    for the selecteditem...

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Splitting string with multiple delimiters

    I can easily do it like this

    row("PODept") = Replace(CmboPODept.SelectedItem.ToString, "'", "`")

    but I know that it not the right way to go

    [edit]Sorry, didn't refresh to see your post, that worked, thanks paul.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: [RESOLVED] Splitting string with multiple delimiters

    Hi paul,

    One thing I noticed is after saving the item, I try to edit it, the dropdowns don't come back with what was saved. It loads like it is a brand new list.
    I've tried both of these

    CmbDept.Text = CStr(data("Department").ToString)
    CmbDept.SelectedItem = CStr(data("Department").ToString)

    doesn't effect the datasource of the combobox. I also tried it after the combo was filled.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Splitting string with multiple delimiters

    after the combo is filled:

    Code:
    CmbDept.SelectedIndex = CmbDept.FindStringExact(data("Department").ToString)

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: [RESOLVED] Splitting string with multiple delimiters

    funny, I was just reading about that method. It finds the correct index but doesn't show it selected in the box

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: [RESOLVED] Splitting string with multiple delimiters

    nevermind paul, it was a PEBKAC lmao

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Splitting string with multiple delimiters

    A what???

  16. #16
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: [RESOLVED] Splitting string with multiple delimiters

    Quote Originally Posted by .paul. View Post
    A what???
    My thoughts exactly.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: [RESOLVED] Splitting string with multiple delimiters

    PEBKAC = Problem exist between keyboard and chair.

    lol

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width