Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Problem parsing a string.

Posted on 2006-10-24
Medium Priority
165 Views
Im parsing a string to add % signs after certain words
Basically if a word is in "double quotes" or 'single quotes' i treat it as a phrase and dont add % to the end
I add % to the end of all other words to make them wildcards for my search.
The problem is that this test string im using wont let me add a % to the last word,

Test string :  Lake ""turtle"" "dog" "cat" 'bat' bat bat12 bat:13 'bat'(horse:bite AND bird (goat OR whale)) NOT Fish
Proper Output:  Lake% ""turtle"" "dog" "cat" 'bat' bat% bat12% bat:13% 'bat'(horse:bite% AND bird% (goat% OR whale%)) NOT Fish%

Here is the code for my project:

Dim i As Integer
Dim SearchString, strNew As String
Dim strAlphaNumeric As String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'()%*+-:" & Chr(34) & Chr(32)
Dim strChar As Char

SearchString = Me.Label1.Text

'strip out non_alphanumeric characters except for my special ones
For i = 1 To Len(SearchString)
strChar = Mid(SearchString, i, 1)
If InStr(strAlphaNumeric, strChar) Then
strNew = strNew + strChar
End If
Next

'add % after everyword that isnt in quotes that isnt AND OR NOT or in quotes
strChar = Nothing
For i = 1 To Len(strNew)
' skip AND, OR, NOT
If Mid(strNew, i, 3) = "AND" Then
i = i + 3

ElseIf Mid(strNew, i, 2) = "OR" Then
i = i + 2

ElseIf Mid(strNew, i, 3) = "NOT" Then
i = i + 3

ElseIf Mid(strNew, i, 1) Like ("[A-Z]") Or Mid(strNew, i, 1) Like ("[a-z]") Or Mid(strNew, i, 1) Like ("#") Then
'chr(40) = "("     chr(41) = ")"       chr(32) = " "
If Mid(strNew, i + 1, 1) = Chr(32) Or Mid(strNew, i + 1, 1) = Chr(40) Or Mid(strNew, i + 1, 1) = Chr(41) Then
strNew = strNew.Insert(i, "%")
i = i + 1
End If
End If

Next

Me.Label2.Text = strNew

0
Question by:bertino12
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Expert Comment

ID: 17796461
Try

dim components() as string
dim s as string

componenets  = Split(Label1.Text, " ")

for each s in components
if not s.EndsWith(Chr(34)) AndAlso not s.EndsWith(Chr(xx)) Then    <--- replace xx with whatever it is for single quote
s = s & "%"
End If
Next

' now build it all back into one string
dim result as string
For each s in Components
result = result & " " & s
Next

MsgBox("Search string is : " & trim(result))

Excuse any typos as this has just been typed stright into here rather than via an editor :)

For each
0

Author Comment

ID: 17798509
For i = 1 To Len(strNew)

I figured it out. The length of the string is changing as items are added to it. the original condition is stored as a constant and never reevaluated in VB .NET
So, I used a while loop and my code now works fine.
0

LVL 1

Accepted Solution

kodiakbear earned 0 total points
ID: 17828800
Closed, 250 points refunded.
kb
Experts Exchange Moderator
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month11 days, 10 hours left to enroll