How to extract just the letters from a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boyntonboy242000
    New Member
    • Apr 2007
    • 6

    How to extract just the letters from a string

    Im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) ..
    So if i put in "The yellow man had 32 teeth!"
    then im trying to get results like" Theyellowmanhad teeth"
    anything besides characters should be stripped out of the inputed field.

    this is in visual basic
  • boyntonboy242000
    New Member
    • Apr 2007
    • 6

    #2
    Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) .. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhad teeth" anything besides characters should be stripped out of the inputed field.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by boyntonboy24200 0
      Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) .. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhad teeth" anything besides characters should be stripped out of the inputed field.
      The simple way would be to loop (probably using a For loop) through each character in the string (most likely using Mid() function) and if it is a letter, append it to another, output string.

      Psedudo-code...
      Code:
      For Each Character In MyInput
        If Ucase(Character) is between "A" and "Z"
          MyOutput = MyOutput & Character
        End If
      Next
      Oh, and by the way - you can't really copy "anything besides characters" because they're all characters.

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        Originally posted by boyntonboy24200 0
        Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) .. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhad teeth" anything besides characters should be stripped out of the inputed field.
        I'm speechless: Killer didn't use Replace! Just brute-force it:
        Code:
        	s = Replace(s, 1, "")
        	s = Replace(s, 2, "")
        	s = Replace(s, 3, "")
        ...
        and, you can put it in a loop so that it is only three lines.

        But, also do it Killer's way so that you can learn how to use the Mid function.

        BTW, if you're using VB.NET, these functions are part of the Strings module, so you say Strings.Mid(... ) and Strings.Replace (...)

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          I think it would be a bit more than three lines, Sammy. Or are you assuming that letters and numbers are the only possible characters?

          Comment

          • dip_developer
            Recognized Expert Contributor
            • Aug 2006
            • 648

            #6
            Originally posted by boyntonboy24200 0
            Im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) ..
            So if i put in "The yellow man had 32 teeth!"
            then im trying to get results like" Theyellowmanhad teeth"
            anything besides characters should be stripped out of the inputed field.

            this is in visual basic
            Take two strings say strChar and strNumber

            Read the length of your string....Loop through your string.....With the Substring() function Read characters one by one........Chec k characters with IsNumeric() Function whether its a number or not.....if number then strip off the character and store it to strNumber Otherwise store it to strChar.......

            after the loop completes strChar will be your string without number.....
            Implement it in code.......

            it will be Something like....

            Code:
            Dim strChar,strNumber As String
            Dim n As Integer=myString.Length
            For i as integer=0 to n-1
            dim str as string=myString.Substring(i,i+1)
            If IsNumeric(str) then
            strNumber+=str
            else
            strChar+=str
            End if
            Next
            MsgBox(strChar)

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by dip_developer
              ...Check characters with IsNumeric() Function whether its a number or not.....if number then strip off the character and store it to strNumber Otherwise store it to strChar.......
              What happens to spaces, and punctuation?

              Comment

              • Geoff
                New Member
                • Feb 2007
                • 17

                #8
                I'd do this with ascii characters for each letter, similar to what was said earlier with between "a" and "z".

                This is proabably over complicating the problem quite a bit, but it works, it keeps the letters in the case they were found in as well ^^

                Code:
                    Dim strText As String, strLeft As String, strRight As String
                    Dim intA As Integer
                    strText = Text1.Text
                    For intA = 1 To Len(strText)
                        strLeft = Left(strText, intA)
                        strRight = Right(strLeft, 1)
                        
                        If Asc(strRight) > 97 And Asc(strRight) < 122 Then
                            Label1.Caption = Label1.Caption & strRight
                        End If
                        If Asc(strRight) > 65 And Asc(strRight) < 90 Then
                            Label1.Caption = Label1.Caption & strRight
                        End If
                    Next intA
                Hope this helps.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by RobertGabriel
                  I have a similar problem. can you guys help?
                  ...
                  If I'm not mistaken, this sounds like a question for the Access forum. Would you like me to move it over there? You're likely to get better information there, if this is SQL-related.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by Geoff
                    I'd do this with ascii characters for each letter, similar to what was said earlier with between "a" and "z".
                    ...
                    Looks good.

                    I do have a couple of issues with it, just on performance grounds. There seems to be a lot of duplicated effort.

                    For example, why use Left() then Right() function? Is this faster than the Mid() function? (this is a serious question - sometimes the longer coding can be more efficient). Or perhaps it's just a personal preference?

                    And your second If test doesn't need to be executed if the first was satisfied. Personally, I'd short-cut the process by using ElseIf or similar. Plus I would place the Asc() value into a variable so the function is only called once.

                    In fact, one of the main reasons I like to use Select Case for these kind of situations is that it allows you much more flexibility in your conditions (if checking one value). For example, where you are doing two IF tests, I would have used something like...
                    Code:
                    Select Case Asc(strRight)
                      Case 97 To 122, 65 To 90
                        ...
                    However, while this seems (to me) "nicer" to code, I don't know how it compares on performance. Must check it out one of these days...

                    Oh, and one more point - I would always use a Long in favour of an Integer, unless space was very tight. Believe it or not, Long data type is (very slightly) faster to use on a 32 bit processor.

                    Comment

                    • RobertGabriel
                      New Member
                      • Apr 2007
                      • 20

                      #11
                      Originally posted by Killer42
                      If I'm not mistaken, this sounds like a question for the Access forum.
                      right. I got carried away.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by RobertGabriel
                        right. I got carried away.
                        Hang on, I'll do a bit of Moderator Magic...

                        Ok, here is the new thread.

                        Comment

                        • dyc
                          New Member
                          • May 2007
                          • 32

                          #13
                          Originally posted by Geoff
                          I'd do this with ascii characters for each letter, similar to what was said earlier with between "a" and "z".

                          This is proabably over complicating the problem quite a bit, but it works, it keeps the letters in the case they were found in as well ^^

                          Code:
                              Dim strText As String, strLeft As String, strRight As String
                              Dim intA As Integer
                              strText = Text1.Text
                              For intA = 1 To Len(strText)
                                  strLeft = Left(strText, intA)
                                  strRight = Right(strLeft, 1)
                                  
                                  If Asc(strRight) > 97 And Asc(strRight) < 122 Then
                                      Label1.Caption = Label1.Caption & strRight
                                  End If
                                  If Asc(strRight) > 65 And Asc(strRight) < 90 Then
                                      Label1.Caption = Label1.Caption & strRight
                                  End If
                              Next intA
                          Hope this helps.
                          Izzit this code is for vb6?in vb 2005 is there also can use the Left method?Thanks

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by dyc
                            Izzit this code is for vb6?in vb 2005 is there also can use the Left method?Thanks
                            Yes, I write in VB6. VB 2005 certainly does have comparable functions, but I don't know the details. I think if you look in the doco, either on your PC or at MS' website, you should find it easily enough.

                            Comment

                            • taylorjpt
                              New Member
                              • Jun 2007
                              • 4

                              #15
                              I always prefer to keep them from putting the characters I don't like into the field in the first place by intercepting the keypress event:

                              [CODE=vb]Private Sub Text1_KeyPress( KeyAscii As Integer)
                              Select Case KeyAscii
                              Case Asc("a") To Asc("z"), Asc("A") To Asc("Z")
                              Case 13
                              'enter key can do something special
                              [user code]
                              keyascii=0 'this keeps the thing from beeping!
                              Case Else
                              KeyAscii = 0
                              End Select
                              End Sub[/CODE]

                              You can then allow back space, delete etc by adding the appropriate character codes. I use this a lot when I just want numbers in auto-calculating forms which I write a lot of.

                              Comment

                              Working...