Solved

issues with formating the strings

Posted on 2008-10-22
16
147 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Kerau
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22778243
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
 
LVL 19

Expert Comment

by:Stephen Manderson
ID: 22778520
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
 

Author Comment

by:Kerau
ID: 22778533
Thanks for your quick reply....Can you please elaborate on this inclined towards my posted example...

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Kerau
ID: 22778586
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
 
LVL 13

Assisted Solution

by:TechTiger007
TechTiger007 earned 166 total points
ID: 22778640
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
 
LVL 19

Assisted Solution

by:Stephen Manderson
Stephen Manderson earned 166 total points
ID: 22778719
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
 
LVL 22

Accepted Solution

by:
prairiedog earned 168 total points
ID: 22779003
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
 

Author Comment

by:Kerau
ID: 22786476

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
 
LVL 22

Expert Comment

by:prairiedog
ID: 22786641
"(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
 

Author Comment

by:Kerau
ID: 22786857
how about something like this

^\([A-Za-z0-9]+\)$
0
 

Author Comment

by:Kerau
ID: 22786948
and how do i validate something like this

(a)(3)(c)
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22786965
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 22786972
>>>and how do i validate something like this (a)(3)(c)
According to your regular expression, it is not a valid input.
0
 

Author Comment

by:Kerau
ID: 22786999
so what should be my expression
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22787102
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
 

Author Closing Comment

by:Kerau
ID: 31508813
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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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