Solved

get rid of ANY spaces SPECIAL CHARACTERS etc in variable

Posted on 2011-02-16
5
842 Views
Last Modified: 2012-05-11
Is there a way in the code below to make sure to no:
padded spaces...extra spaces..special characters do not get added as the variable is being passed along
in the function below ?

Thanks
fordraiders
Private Sub PosCompartAdvance()
        If Me.CheckBox7.Checked = True Then          ' Position
            ' Me.FldPositionTextBox.Text = mlngData7
            If Me.CheckBox9.Checked = True Then
                Me.FldPositionTextBox.Text = mlngData7  ' Position
                Dim arg As Char = Trim(FldPositionTextBox.Text)
                If Char.IsLetter(arg) Then
                    Dim value As Integer = AscW(arg)
                    Select Case value
                        Case 65 To 89, 97 To 121
                            FldPositionTextBox.Text = Trim(ChrW(value + 1).ToString)
                    End Select
                End If

                If Char.IsNumber(arg) Then
                    Dim value As Integer
                    ' Dim value As Integer = CInt(FldPositionTextBox.Text).ToString ' added to string here 01/25/2011
                    'Dim value As Integer = Trim(Integer.Parse(FldPositionTextBox.Text))
                    value = Trim(Integer.Parse(FldPositionTextBox.Text))

                    Select Case value
                        Case Is > 0 ' or Case 1 To 1000 
                            value += 1
                            FldPositionTextBox.Text = value.ToString
                            If Me.FldPositionTextBox.Text = "1" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "2" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "3" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "4" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "5" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "6" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "7" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "8" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                            If Me.FldPositionTextBox.Text = "9" Then Me.FldPositionTextBox.Text = "0" + Me.FldPositionTextBox.Text
                    End Select
                End If

            End If

        Else
            ' else not checked

        End If


        If Me.CheckBox8.Checked = True Then          ' Compartment
            ' Me.FldCompartmentTextBox.Text = mlngData8
            If Me.CheckBox10.Checked = True Then
                Me.FldCompartmentTextBox.Text = mlngData8  ' Compartment
                Dim arg As Char = FldCompartmentTextBox.Text
                If Char.IsLetter(arg) Then
                    Dim value As Integer = AscW(arg)
                    Select Case value
                        Case 65 To 89, 97 To 121
                            '            value = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, "[^a-zA-Z0-9]", String.Empty)
                            FldCompartmentTextBox.Text = ChrW(value + 1).ToString
                    End Select
                End If

                If Char.IsNumber(arg) Then
                    '  Dim value As Integer = CInt(FldCompartmentTextBox.Text)
                    Dim value As Integer = Integer.Parse(FldCompartmentTextBox.Text)

                    Select Case value
                        Case Is > 0 ' or Case 1 To 1000 
                            value += 1
                            FldCompartmentTextBox.Text = value.ToString
                            If Me.FldCompartmentTextBox.Text = "1" Then FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "2" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "3" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "4" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "5" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "6" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "7" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "8" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "9" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                    End Select
                End If

            End If
        Else
            ' else not checked
            ' FldCompartmentTextBox.Clear()
        End If

    End Sub

Open in new window

0
Comment
Question by:fordraiders
5 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
Comment Utility
Comprehensive discussion: http://dotnetperls.com/whitespace this is all C# easily converted to VB.Net with online tools.

Here's VB.Net and this example is very thorough.

http://www.dreamincode.net/code/snippet3423.htm

This article takes a fairly in-depth look at strings. Ideally you'd want to parse the string a number of ways. Looking for " " a space the ASCII characters for space, tab and return and then you'd want to also search for "\r", "\s", "\n" and "\t" you could of course use RegEx as well.

Here is a bunch of solid examples from Google with this search term:
http://www.google.com/search?q=vb.net+code+to+replace+all+space+and+tabs&rls=com.microsoft:en-us&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1&rlz=1I7GGLL_en#q=vb.net+code+to+replace+all+space+in+strings&hl=en&rls=com.microsoft:en-us&rlz=1I7GGLL_en&prmd=ivns&ei=PkZcTfr-M468sQOe_9zwAQ&start=0&sa=N&fp=cb921b9e247079c3

0
 
LVL 1

Assisted Solution

by:HumbleBeginnings
HumbleBeginnings earned 250 total points
Comment Utility
Hi Fordraiders,

I'm not sure exactly what your code is trying to accomplish, but based on your question/request you might want to try working this into your code.

If you want to eliminate all characters except numbers, letters and spaces (but trim leading and trailing spaces) try this:

      FldPositionTextBox.Text = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, "[^a-zA-Z0-9\s]", "").Trim
      FldPositionTextBox.Text = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, " {2,}", " ")

If you want all "printable" characters and spaces (but again trim all leading and trailing spaces) try this:

      FldPositionTextBox.Text = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, "[^\040-\176]", "").Trim
      FldPositionTextBox.Text = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, " {2,}", " ")

Please note that the second line in each example replaces multiple spaces with a single space. There may be a more elegant way to do this in Regex but at least this will get you going. If I understood your request correctly, you seemed to have been pretty close with your Regex line that was commented out.

Again, I hope that I understood your request correctly and I hope that this helps. :)
0
 
LVL 3

Author Comment

by:fordraiders
Comment Utility
you want to eliminate all characters except numbers, letters trim leading and trailing spaces and extra spaces

0
 
LVL 12

Accepted Solution

by:
Hairbrush earned 250 total points
Comment Utility
fordraiders, when you say "extra spaces", we are interpreting this to mean that single spaces are ok but "extra" i.e. duplicate spaces are not... " " ok but "  " not ok.  Is that correct?  If so, HumbleBeginnings' response will do what you ask.

If you want to remove all spaces, then change

FldPositionTextBox.Text = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, "[^a-zA-Z0-9\s]", "").Trim

to

FldPositionTextBox.Text = System.Text.RegularExpressions.Regex.Replace(FldPositionTextBox.Text, "[^a-zA-Z0-9]", "").Trim

By the way, this block of code:

                            If Me.FldCompartmentTextBox.Text = "1" Then FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "2" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "3" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "4" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "5" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "6" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "7" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "8" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text
                            If Me.FldCompartmentTextBox.Text = "9" Then Me.FldCompartmentTextBox.Text = "0" + Me.FldCompartmentTextBox.Text

can be shortened to:

If Regex.IsMatch(FldCompartmentTextBox.Text, "[1-9]") Then FldCompartmentTextBox.Text = "0" & FldCompartmentTextBox.Text

or if you prefer:

If FldCompartmentTextBox.Text Like "[1-9]" Then FldCompartmentTextBox.Text = "0" & FldCompartmentTextBox.Text

in fact, since you've just assigned the value of FldCompartmentTextBox.Text you could do the following:

If value > 0 AndAlso value < 10 then FldCompartmentTextBox.Text = "0" & value.tostring
0
 
LVL 3

Author Closing Comment

by:fordraiders
Comment Utility
Thanks
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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.

744 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

7 Experts available now in Live!

Get 1:1 Help Now