I am trying to split a string based on the - character and insert the words before and after the - character into a list (result) both words with different indexes. What I am trying to achieve is to check whether there is a space before and after the character -. If there is space found, perform the splitting as mentioned earlier. Otherwise, if there is no space before or after -, do not perform any splitting.
Example:
String1 = London - United Kingdom
String2 = Paris-France
Split String1 and insert London with index(0) and United Kingdom with index(1) into the result list because there is space before and after the -
Do not split String 2 and insert Paris-France with index(0)because there is no space before and after -.
Code:
Dim result As New List(Of String)()
For Each str_get As String In Split
If (str_get.IndexOf("\t-\t")) Then
Dim splitStr = str_get.Split({"-", "/"}, StringSplitOptions.None)
For Each str_split As String In splitStr 'Add to result list
result.Add(str_split.Trim()) ' Enter into result list
' result.TrimExcess()
Next
Else
End If
Split Is a string which may be considered as an array of strings. The For loop is to take each string in that Array and check it.
Any Thoughts or suggestions ?
Simply try splitting on " - " instead of "-". It should work
EDIT:
Ok, just verified that String.Split doesn't work that way (with a String separator)
This will work:
splitStr = Regex.Split(str_get, " - ")
Demo on Ideone
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With