Question String split failed

LeonLanford

Active member
Joined
Dec 2, 2008
Messages
28
Programming Experience
Beginner
I'm really confused.. 2 hours working with this and still cannot solve it :(

I have this code
VB.NET:
        Using WC As New System.Net.WebClient()
            Dim strem As IO.Stream = WC.OpenRead(alamat_view)
            Using reader As New System.IO.StreamReader(strem)
                Dim line As String = String.Empty

                baris = 0

                While reader.Peek >= 0

                    line = reader.ReadLine()

                    Dim kalimat() As String = line.Split("||")

                    dgv_view.Rows.Add()
                    dgv_view.Item(0, baris).Value = baris + 1
                    'dgv_view.Item(1, baris).Value = Path.GetFileName(line)
                    dgv_view.Item(2, baris).Value = host & line
                    baris += 1

                End While
            End Using
        End Using

I want to split "line" to array of "kalimat"
the string example is "uplot/leon.kacau.ayam.ass||1k||07-Dec-2008_02:25"

I tested by putting these
VB.NET:
                    MessageBox.Show(kalimat(0))
                    MessageBox.Show(kalimat(1))
                    MessageBox.Show(kalimat(2))
                    MessageBox.Show(kalimat(3))
                    MessageBox.Show(kalimat(4))

kalimat(0) shows "uplot/leon.kacau.ayam.ass"
kalimat(1) shows ""
kalimat(2) shows "1k"
kalimat(3) shows ""
kalimat(4) shows "07-Dec-2008_02:25"

--

What I want to ask is why there's gap between them? 1 and 3 empty.. I'm having problem with big string if there's gap like that.. How I can delete the gap?

Thanks..
 
There is no String.Split overload that takes a single string as parameter, so what you get in your case is some kind of conversion from the given string to one of the valid parameter types that Split allows. If you really want to use a string for split you can use a method that takes a string array:
VB.NET:
Dim parts() As String = line.Split(New String() {"||"}, StringSplitOptions.None)
 
There is no String.Split overload that takes a single string as parameter, so what you get in your case is some kind of conversion from the given string to one of the valid parameter types that Split allows. If you really want to use a string for split you can use a method that takes a string array:
VB.NET:
Dim parts() As String = line.Split(New String() {"||"}, StringSplitOptions.None)

Thanks.. it's worked :D
What's the difference between it and StringSplitOptions.RemoveEmptyEntries?
 
RemoveEmptyEntries removes entries that are empty after the split. For example, if you got string "||1k||" and split by "||" you get three entries in result array, the "1k" and two empty zero length strings. RemoveEmptyEntries will remove those empty entries and the returned string array will only contain the one entry "1k".
 
There is no String.Split overload that takes a single string as parameter, so what you get in your case is some kind of conversion from the given string to one of the valid parameter types that Split allows.

I'd presume that VB is allowing implicit conversion from String to Char[], and passing an array of two "|"c characters

Yuck
 
I'd presume that VB is allowing implicit conversion from String to Char[], and passing an array of two "|"c characters

Yuck
No, an implicit conversion was done, but not what you think, and it really is not relevant what does happen in that case because the given input parameter was wrong according to the allowed types, the only way to fix it is to use correct parameters. Turning on Option Strict would give you notification here that there was not made an explicit input that the compiler could understand, which would force you to fix the error.
 

Latest posts

Back
Top