Solved

issues with formating the strings

Posted on 2008-10-22
16
145 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

19 Experts available now in Live!

Get 1:1 Help Now