How to parse a string with double quotes
-
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
-
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
hi have you test the String.Split method? or you need something more than it?
-
hi have you test the String.Split method? or you need something more than it?
-
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.
-
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
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
-
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
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 -
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
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