Solved

get rid of ANY spaces SPECIAL CHARACTERS etc in variable

Posted on 2011-02-16
5
843 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
ID: 34911425
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
ID: 34913237
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
ID: 34913642
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
ID: 34914836
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
ID: 34943160
Thanks
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

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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