Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. string tokenization

string tokenization

Scheduled Pinned Locked Moved Visual Basic
helpcsharpcomregex
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hsprasain
    wrote on last edited by
    #1

    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

    T 2 Replies Last reply
    0
    • H 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

      T Offline
      T Offline
      TwoFaced
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • H 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

        T Offline
        T Offline
        TwoFaced
        wrote on last edited by
        #3

        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
        
        1 Reply Last reply
        0
        • T TwoFaced

          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.

          H Offline
          H Offline
          hsprasain
          wrote on last edited by
          #4

          Thanks a lot friend!!! I appreciate your help. It works well now.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups