Solved

get rid of ANY spaces SPECIAL CHARACTERS etc in variable

Posted on 2011-02-16
5
845 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

16 Experts available now in Live!

Get 1:1 Help Now