issues with formating the strings

hi,

I am working in VB.Net and i am struggling to format String to insert value in the database. The issue is i want to format string RuleNumberLocal to first check if the parenthesis is being added by the user or not and if not add parenthesis to the value inserted by a user. For eg. A user inserts "a" (without quotes) in the txtbox. So before inserting this value "a", it should check if the parenthesis is being added by the user i.e. (a), if not, add the parenthesis (a) to the database.

If user inserts a value (a) with parenthesis, don't add anything and let it go to the database directly. How do i achieve this, please suggest.
If Not String.IsNullOrEmpty(LocalRuleNumber) Then
            strQueryText.AppendLine("rt.RuleCategory = 'Local' AND ")
            strQueryText.AppendLine("UPPER(rth.LR_RuleNumberState + rth.LR_RuleNumberLocal) LIKE @LocalRuleNumber AND ")
            objDP.AddParameter("@LocalRuleNumber", String.Format("%{0}%", LocalRuleNumber.ToUpper), GetType(String))
        End If

Open in new window

KerauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TechTiger007Commented:
here is a sample

dim str as string = "(a)";
        if (str.IndexOf("(") != 0) then
            str = "(" & str
end if
        if (str.LastIndexOf(")") != str.Length - 1) then
            str = str & ")"
end if
0
Stephen MandersonSoftware EngineerCommented:
Do you have exact values for the local rule number? If so you can use regex to match the exact format you require.

Heres an example..

Dim LocalRuleNumber As String = "(a"

        If Not HasParenthesis(LocalRuleNumber) Then
            LocalRuleNumber = SetParenthesis(LocalRuleNumber)
        End If

Would set LocalRuleNumber = "(a)"

It would strip all brackets that dont match the regex pattern and then recreated them

Regards
Steve
Imports System.Text.RegularExpressions
 
    Public Function HasParenthesis(ByVal StringToCheck As String) As Boolean
 
        Dim Format As New Regex("\([a-zA-Z]*\)")
        Return Format.IsMatch(StringToCheck)
 
    End Function
 
    Public Function SetParenthesis(ByVal StringToConvert As String) As String
 
        StringToConvert = StringToConvert.Replace("(", "")
        StringToConvert = StringToConvert.Replace(")", "")
        StringToConvert = "(" & StringToConvert & ")"
        Return StringToConvert
 
    End Function

Open in new window

0
KerauAuthor Commented:
Thanks for your quick reply....Can you please elaborate on this inclined towards my posted example...

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

KerauAuthor Commented:
Steve,

I don't have exact values for my local rule number. what i could say at this point is: they will be from a-z or A-Z

Thanks
0
TechTiger007Commented:
the code below will add ( to the beginning of the LocalRuleNumber if it is not already there and ) to the end
dim str as string = LocalRuleNumber

        if (str.IndexOf("(") != 0) then
            str = "(" & str
end if
        if (str.LastIndexOf(")") != str.Length - 1) then
            str = str & ")"
end if

objDP.AddParameter("@LocalRuleNumber", String.Format("%{0}%", str.ToUpper), GetType(String))

             
 

0
Stephen MandersonSoftware EngineerCommented:
Heres some examples of what the regex format matching can do..

Dim Format As New Regex("\([a-zA-Z]*\)")
allows "abc..xyz", "ABC..XYZ", "aBc..XyZ"  (* represents a string of any given size)

If you want to allow spaces in the string then add a space after Z or to include numbers 0-9 after A-Z

Providing you have the 2 functions included as well as the regex import then it can be used like this..

Regards
Steve
        If Not String.IsNullOrEmpty(LocalRuleNumber) Then
            If Not HasParenthesis(LocalRuleNumber) Then
                LocalRuleNumber = SetParenthesis(LocalRuleNumber)
            End If
            strQueryText.AppendLine("rt.RuleCategory = 'Local' AND ")
            strQueryText.AppendLine("UPPER(rth.LR_RuleNumberState + rth.LR_RuleNumberLocal) LIKE @LocalRuleNumber AND ")
            objDP.AddParameter("@LocalRuleNumber", String.Format("%{0}%", LocalRuleNumber.ToUpper), GetType(String))
        End If

Open in new window

0
prairiedogCommented:
It is going to be very painful to do this kind of validation based on the current information you provided. Say if user enters this:
(I)(am)(hacker)
How are you goging to validate and format it?
You need to give more information about this field so we can better help you. For example, the length, allowed characters, etc.
Also, if the RuleNumberLocal is a small collection that you already know, then make it a DropDownList to let user choose from instead of letting user type in.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KerauAuthor Commented:

This is truly a place where experts exchange their ideas. You are correct prairiedog, you gave me a whole new direction towards my thinking to validate. Instead of taking all the crap in from the user, why not validate before you take in anything.And No i don't have the option to create a drop-down list right away. This is something i can put forward to my supervisior to think about.

Here is what i did i went to regular expression validator to validate parenthsis and i am using the following expression \([A-Za-z]+\). Again the issue would be; how do i validate this (I)(am)(hacker) or even this (I)(2)(hacker). For sure parenthesis should be there after every entry. Is there a way/expression to validate this? or i am beating around bushes :)

p.s MrManderson and techtiger007 you both are equally great and i really appreciate your effort and time, i will assign points for each and every contributions you have made.

thanks
0
prairiedogCommented:
"(I)(am)(hacker)" does not pass your Regular Express Validator, so you can rest assured :-).
But it will be very nice if you know the max length of the letters allowed, or even what letters are allowed and what letters are not allowed, so you can fine-tuen your validator.
You should begin your regular express with a "^" character and end it with a "$":
^\([A-Za-z]+\)$
0
KerauAuthor Commented:
how about something like this

^\([A-Za-z0-9]+\)$
0
KerauAuthor Commented:
and how do i validate something like this

(a)(3)(c)
0
prairiedogCommented:
I don't know your rules. But if you allow numbers, it should work. But keep it in mind it will match a wide range of input: (0), (00), (00000000), (a0), (a00b), (A00a), (Ab0c), (Z0000000000000000000ABCadefg), etc will all be valid input.
0
prairiedogCommented:
>>>and how do i validate something like this (a)(3)(c)
According to your regular expression, it is not a valid input.
0
KerauAuthor Commented:
so what should be my expression
0
prairiedogCommented:
Honestly, I don't know, because I don't know what rules you have, what format is allowed, what format is not allowed. For example, if (a)(3)(c) is allowed, then how about (a)(c)? You need to have a specific rule in order to use regular expression.
0
KerauAuthor Commented:
Thank you guys, I believe i should start a new thread as I am deviating from my original question. I have tried to reward points equally, since prairiedog's suggestion is the one i am going with right now so he gets 2 more points ....;-)

Thanks again for participating and pouring in your valuable ideas...
Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.