Solved

Problem parsing a string.

Posted on 2006-10-24
4
147 Views
Last Modified: 2010-04-23
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
                    'add %
                    strNew = strNew.Insert(i, "%")
                    i = i + 1
                End If
            End If

        Next

        Me.Label2.Text = strNew

I appreciate any help you guys can give. Thanks,
0
Comment
Question by:bertino12
4 Comments
 
LVL 6

Expert Comment

by:cubixSoftware
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

by:bertino12
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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Paging GridView 7 48
DataGridview Currency Formating? 22 52
VB.Net Serial COM1 Port - how to send this text via Serial COM port? 13 41
Get Top Visible Row of Datagridview 6 26
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now