[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 :wave:
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
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
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!! :D