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. How to parse a string with double quotes

How to parse a string with double quotes

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
7 Posts 6 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.
  • J Offline
    J Offline
    j1webb
    wrote on last edited by
    #1

    I'm interested in turning this type of string - blah, A&Z this "some word", another word, "Joe Jones" into "searchable" tokens, e.g. blah A&Z this some word another word Joe Jones The main delimiters would be whitespace and/or comma's, with quotes allowing for multiple words in a token. Does anyone know how this might be done? -- modified at 15:45 Tuesday 27th September, 2005

    N _ E J 4 Replies Last reply
    0
    • J j1webb

      I'm interested in turning this type of string - blah, A&Z this "some word", another word, "Joe Jones" into "searchable" tokens, e.g. blah A&Z this some word another word Joe Jones The main delimiters would be whitespace and/or comma's, with quotes allowing for multiple words in a token. Does anyone know how this might be done? -- modified at 15:45 Tuesday 27th September, 2005

      N Offline
      N Offline
      namazikhah
      wrote on last edited by
      #2

      hi have you test the String.Split method? or you need something more than it?

      C 1 Reply Last reply
      0
      • N namazikhah

        hi have you test the String.Split method? or you need something more than it?

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Yes, he does. He wants to ensure that quoted items are not split if they contain the split character.


        My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

        R 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Yes, he does. He wants to ensure that quoted items are not split if they contain the split character.


          My: Blog | Photos "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious

          R Offline
          R Offline
          rwestgraham
          wrote on last edited by
          #4

          I think the simplest parsing algorithm for this scenario is: 1) Split on blank spaces to get an initial array quickly. 2) Create a second ArrayList so you don't have to worry about Redims. 3) Enumerate the intial array, test each item for both " and nulls - the person may have entered two blanks by accident. Copy "standalone" strings into the ArrayList. When you encounter a ", start reconcatenation of the original string until you reach the element that contains the closing ". Add the reconstituted string to the ArrayList... etc. until you have an ArrayList where each element represents an original search term.

          1 Reply Last reply
          0
          • J j1webb

            I'm interested in turning this type of string - blah, A&Z this "some word", another word, "Joe Jones" into "searchable" tokens, e.g. blah A&Z this some word another word Joe Jones The main delimiters would be whitespace and/or comma's, with quotes allowing for multiple words in a token. Does anyone know how this might be done? -- modified at 15:45 Tuesday 27th September, 2005

            _ Offline
            _ Offline
            _mubashir
            wrote on last edited by
            #5

            Hello! Look at the 'Split' property of strings.. When you split the string with 'whitespace' it returns an array of strings that are splitted apart by whitespace. e.g if str is your string then dim s() as string=str.Split(" ") now s(0) will contain "Blah" s(1) will contain "A&Z" and so on... Hope this will solve your problem _mubashir

            1 Reply Last reply
            0
            • J j1webb

              I'm interested in turning this type of string - blah, A&Z this "some word", another word, "Joe Jones" into "searchable" tokens, e.g. blah A&Z this some word another word Joe Jones The main delimiters would be whitespace and/or comma's, with quotes allowing for multiple words in a token. Does anyone know how this might be done? -- modified at 15:45 Tuesday 27th September, 2005

              E Offline
              E Offline
              Edbert P
              wrote on last edited by
              #6

              I can think of at least two ways, though not sure of the efficiency ratings of them: First solution: 1. Use RegEx to get collection of words paired by " (double quotes), then remove the words from the string (i.e. replace with empty string). This is possible using RegEx AFAIK. 2. Now you have blah, A&Z this , another word, left, replace comma with space (or vice versa). Split the string using space character and get all arrays that are not empty. Second solution: Create a parser to loop through the characters in the string. Everytime you encounter a comma or blank space save the characters you have read as a string, with the exception when you encounter " (double quotes) then continue until you find another double quotes (use a boolean variable for this). Hope that helps, Ed

              1 Reply Last reply
              0
              • J j1webb

                I'm interested in turning this type of string - blah, A&Z this "some word", another word, "Joe Jones" into "searchable" tokens, e.g. blah A&Z this some word another word Joe Jones The main delimiters would be whitespace and/or comma's, with quotes allowing for multiple words in a token. Does anyone know how this might be done? -- modified at 15:45 Tuesday 27th September, 2005

                J Offline
                J Offline
                j1webb
                wrote on last edited by
                #7

                I really appreciate all of the help and it gave me some good ideas which I was able to apply and solve my problem. For me, the key was removing the "phrases" from the string and placing them in their own array. First I counted the number of double quotes and divided by 2. That gave me the number of "phrases" I had in the string. Then I split the string using doublequotes as the delimiter, but I did this one at a time and then removed the "phrase" from the original string, placed it in an array and then split the string again. Dim doublequote As String = Chr(34) If InStr(Keyword, doublequote) Then For i = 0 To intQuoteCount - 1 Dim arrFilename() As String arrFilename = Split(Keyword, doublequote) strRemainder0 = arrFilename(0).Trim strRemainder1 = arrFilename(1).Trim strPhrase(i) = strRemainder1 Keyword = Replace(Keyword, strRemainder1, "") strTmp = doublequote & doublequote Keyword = Replace(Keyword, strTmp, "") Next i Keyword = Replace(Keyword, doublequote, "") End If Then I split the string using " " as the delimiter. That put all of the words that were left in another array, KeywordsForSearch. Then I expanded that array by the number of elements I had in the strPhrase array and then added the elements from the strPhrase array to the KeywordsForSearch array. ' Keyword = Replace(Keyword, ",", " ") ' Just in case there are some single quotes in the search string Keyword = Replace(Keyword, "'", "''") ' In case there are double spaces in the string Keyword = Replace(Keyword, " ", " ") 'Read in the search words to be searched KeywordsForSearch = Split(Trim(Keyword), " ") Keyword = Replace(Keyword, "''", "'") Dim strKeyWord As String Dim intArrayCount As Integer = UBound(KeywordsForSearch) intArrayCount = intArrayCount + intQuoteCount ReDim Preserve KeywordsForSearch(intArrayCount) i = 0 intArrayCount = intArrayCount - 1 For i = 0 To intQuoteCount - 1 KeywordsForSearch(intArrayCount) = strPhrase(i) intArrayCount = intArrayCount + 1 Next i I'm sure this is not the most elegant solution and someone with more logical thought processes could probably clean this up considerably, but it does work. Perhaps someone else mi

                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