string tokenization
-
Hi all, I would be really thankful to you all if I could help me to slove my the following problem. I've written following code to tokenize my string in VB.NET which use " " i.e space as delimiters. Dim data(100,100) as string Dim value As String = "&&&&&&&&&&&&&& Tech&&&&&&&&&Repu &&&& blic.com" # Input string where & is a space. I used & as space while posting message since space was ignored while posting message Dim pattern As String = " " # delimiters sites = value.Split(pattern) k = 0 For Each s In sites If s <> pattern Then data(0, k) = s outFile.WriteLine(data(0, k)) k = k + 1 Else MessageBox.Show(" Space found") End If Next s According to my understanding, the program should ignore the space and assign data(0,0)=Tech data(0,1)=Repu data(0,2)=blic.com But unfortunately, else part of code is never executed and some spaces are stored on dataarray along with the string. Can anybody help me to achieve me my goal or any other advise for me? My Goal is to assign: data(0,0)=Tech data(0,1)=Repu data(0,2)=blic.com hsprasain
-
Hi all, I would be really thankful to you all if I could help me to slove my the following problem. I've written following code to tokenize my string in VB.NET which use " " i.e space as delimiters. Dim data(100,100) as string Dim value As String = "&&&&&&&&&&&&&& Tech&&&&&&&&&Repu &&&& blic.com" # Input string where & is a space. I used & as space while posting message since space was ignored while posting message Dim pattern As String = " " # delimiters sites = value.Split(pattern) k = 0 For Each s In sites If s <> pattern Then data(0, k) = s outFile.WriteLine(data(0, k)) k = k + 1 Else MessageBox.Show(" Space found") End If Next s According to my understanding, the program should ignore the space and assign data(0,0)=Tech data(0,1)=Repu data(0,2)=blic.com But unfortunately, else part of code is never executed and some spaces are stored on dataarray along with the string. Can anybody help me to achieve me my goal or any other advise for me? My Goal is to assign: data(0,0)=Tech data(0,1)=Repu data(0,2)=blic.com hsprasain
Because your using spaces as your delimiter it was probably hard to notice but the values that will be contained in 'sites' are not spaces, they are empty strings. Take a look at this code:
Dim value As String = "A,,B,C" For Each s As String In value.Split(","c) Console.WriteLine(s) Next
You'll notice the values returned by the split function are 'A','','B','C' where '' is an empty string. It doesn't return the delimiter. In your loop you should test that the string is not empty.
-
Hi all, I would be really thankful to you all if I could help me to slove my the following problem. I've written following code to tokenize my string in VB.NET which use " " i.e space as delimiters. Dim data(100,100) as string Dim value As String = "&&&&&&&&&&&&&& Tech&&&&&&&&&Repu &&&& blic.com" # Input string where & is a space. I used & as space while posting message since space was ignored while posting message Dim pattern As String = " " # delimiters sites = value.Split(pattern) k = 0 For Each s In sites If s <> pattern Then data(0, k) = s outFile.WriteLine(data(0, k)) k = k + 1 Else MessageBox.Show(" Space found") End If Next s According to my understanding, the program should ignore the space and assign data(0,0)=Tech data(0,1)=Repu data(0,2)=blic.com But unfortunately, else part of code is never executed and some spaces are stored on dataarray along with the string. Can anybody help me to achieve me my goal or any other advise for me? My Goal is to assign: data(0,0)=Tech data(0,1)=Repu data(0,2)=blic.com hsprasain
Also you could use a regular expression to do the splitting. It will allow you to remove all the white space between values so you only get the actual text. Take a look at this.
Dim value As String = "A B C" Dim regex As New System.Text.RegularExpressions.Regex("\\s+") For Each s As String In regex.Split(value) Console.WriteLine(s) Next
-
Because your using spaces as your delimiter it was probably hard to notice but the values that will be contained in 'sites' are not spaces, they are empty strings. Take a look at this code:
Dim value As String = "A,,B,C" For Each s As String In value.Split(","c) Console.WriteLine(s) Next
You'll notice the values returned by the split function are 'A','','B','C' where '' is an empty string. It doesn't return the delimiter. In your loop you should test that the string is not empty.