Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Splitting a String with a variable delimiter

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    11

    Resolved [RESOLVED] [2008] Splitting a String with a variable delimiter

    Hello. Really been struggling with this one, and i cant seem to find a simple method of doing it...surely there must be one? I have a list of values that i am loading into a variable. I am trying to split that variable by the whitespace that separates each value...the only thing is the whitespace varies in size. eg...

    value 1_______________value 2____value 3
    value 4__value5____________value6

    is there any way of splitting by batches of whitespace?

    Thank you

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2008] Splitting a String with a variable delimiter

    You could substitute the delimiter out so you only have one and then split the string. You can also do this;

    Code:
            Dim s As String = "value 1_______________value 2____value 3"
            Dim split As Char() = {"_"c}
            Dim splits() As String = s.Split(split, System.StringSplitOptions.RemoveEmptyEntries)
            For Each str As String In splits
                MessageBox.Show(str)
            Next

  3. #3
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Splitting a String with a variable delimiter

    You could use LINQ for that.

    Example.
    Code:
    Dim value As String = "1               value2    value3"
            
            Dim list = From s In value.Split(" "c) _
                         Where s.Length > 0

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    11

    Re: [2008] Splitting a String with a variable delimiter

    Quote Originally Posted by Bulldog
    You could substitute the delimiter out so you only have one and then split the string. You can also do this;

    Code:
            Dim s As String = "value 1_______________value 2____value 3"
            Dim split As Char() = {"_"c}
            Dim splits() As String = s.Split(split, System.StringSplitOptions.RemoveEmptyEntries)
            For Each str As String In splits
                MessageBox.Show(str)
            Next
    Fantastic, that worked a charm. Thank you!!

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