locked
split string RRS feed

  • Question

  • Hi i tried to split the following string

    Dim test as String = "col:val::col:val::col:val"

    Dim ColsVals() as String = test.Split("::")

    Expected result

    ColsVals(0) = "col:val"

    ColsVals(1) = "col:val"

    ColsVals(2) = "col:val"

    but i am getting following result

    ColsVals(0) = "col"

    ColsVals(1) = "val"

    ColsVals(2) = "col"

    ColsVals(3) = "val"

    ColsVals(4) = "col"

    ColsVals(5) = "val"

    Sunday, October 13, 2013 12:01 PM

Answers

  • Expected result

    ColsVals(0) = "col:val"

    You are using the Char version of the string split method, and the compiler is silently doing a conversion from string to char in the background.  So your code is effectively

        Dim ColsVals() as String = test.Split(":"c)

    which gives the result you are seeing.  I would recommend setting Option Strict On at the top of your code.

    If you want to use a string as a delimiter, it must be a string array, even if the array is one element long.

            Dim test As String = "col:val::col:val::col:val"
            Dim Delim() As String = New String() {"::"}
            Dim ColsVals() As String = test.Split(Delim, StringSplitOptions.RemoveEmptyEntries)
    

    • Marked as answer by sivakl_2001 Sunday, October 13, 2013 12:28 PM
    Sunday, October 13, 2013 12:17 PM

All replies

  • Hope this helps

     Dim test As String = "col:val::col:val::col:val"
    
            Dim ColsVals() As String = RegularExpressions.Regex.Split(test, "::")
    
    
            Dim part As String
            For Each part In ColsVals
                Console.WriteLine(part)
            Next
    

    Sunday, October 13, 2013 12:14 PM
  • You are using the Char version of the string split method, and the compiler is silently doing a conversion from string to char in the background.  So your code is effectively

        Dim ColsVals() as String = test.Split(":"c)

    which gives the result you are seeing.  I would recommend setting Option Strict On at the top of your code.

    If you want to use a string as a delimiter, it must be a string array, even if the array is one element long.

            Dim test As String = "col:val::col:val::col:val"
            Dim Delim() As String = New String() {"::"}
            Dim ColsVals() As String = test.Split(Delim, StringSplitOptions.RemoveEmptyEntries)
    

    • Proposed as answer by Frank L. Smith Sunday, October 13, 2013 12:26 PM
    Sunday, October 13, 2013 12:16 PM
  • Expected result

    ColsVals(0) = "col:val"

    You are using the Char version of the string split method, and the compiler is silently doing a conversion from string to char in the background.  So your code is effectively

        Dim ColsVals() as String = test.Split(":"c)

    which gives the result you are seeing.  I would recommend setting Option Strict On at the top of your code.

    If you want to use a string as a delimiter, it must be a string array, even if the array is one element long.

            Dim test As String = "col:val::col:val::col:val"
            Dim Delim() As String = New String() {"::"}
            Dim ColsVals() As String = test.Split(Delim, StringSplitOptions.RemoveEmptyEntries)
    

    • Marked as answer by sivakl_2001 Sunday, October 13, 2013 12:28 PM
    Sunday, October 13, 2013 12:17 PM
  • Hi,

     Here is one more way you can do it.

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim test As String = "col:val::col:val::col:val"
    
            Dim ColsVals() As String = Split(test, "::")
    
            For Each s As String In ColsVals
                ListBox1.Items.Add(s)
            Next
        End Sub
    

    Here is the result listed in a listbox

    Sunday, October 13, 2013 12:29 PM