Results 1 to 6 of 6

Thread: [RESOLVED] find and list only letters/numbers in string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Resolved [RESOLVED] find and list only letters/numbers in string

    if a string has letters, numbers, and other characters, such as "N23@xa1#P4a!", can the other characters be removed and the letters/numbers be listed in a multi-line textbox such as below;

    N23
    xa1
    P4a

    I've been messing around with InString and Mid functions, but cant come up with a practical way to do it.
    I can easily remove the other characters from a string, but how to list the others is getting tricky.

    I'm going to try Replacing the other characters with a KeyAscii Enter code and then send the string to the multi-line textbox. (not sure if that will work????)

    Any other ideas?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Re: find and list only letters/numbers in string

    Well, that worked...

    Code:
    Text2.Text = Replace$(Text1.Text, "@", vbCrLf)

  3. #3
    Addicted Member sergeos's Avatar
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    162

    Re: [RESOLVED] find and list only letters/numbers in string

    Try this code.
    Code:
    Public Function TrimNonNumeric(ByVal strData As String) As String
        Const NUMERIC_CHARS = "0123456789ABC"   '<-TYPE ALL NEEDED CHARS
        Dim lThisChar   As Long
        Dim sTemp       As String
        Dim bAdd         As Boolean
        
        For lThisChar = 1 To Len(strData)
            If InStr(1, NUMERIC_CHARS, Mid$(strData, lThisChar, 1)) > 0 Then
                sTemp = sTemp & Mid$(strData, lThisChar, 1)
            Else
                bAdd = True
                'If sTemp <> vbNullString Then Exit For
            End If
            If bAdd Or Len(sTemp) > 0 Then
                    TextBox1.Text = TextBox1.Text & sTemp & vbCrLf
                    bArr = False
                    sTemp = vbNullString
            End If
        Next
        TrimNonNumeric = sTemp
    End Function
    Ten Years After - 01 You Give Me Loving

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: [RESOLVED] find and list only letters/numbers in string

    Here's a couple of generic routines I have. I'm not going to say they're the fastest way to get it done, but they work:

    Code:
    
    
    Public Function sFilter(sString As String, sAllowed As String) As String
        ' This filters all but the characters in sAllowed from the sString string.
        Dim sOut As String
        Dim s As String * 1
        Dim i As Integer
        '
        For i = 1 To Len(sString)
            s = mid(sString, i, 1)
            If InStr(sAllowed, s) <> 0 Then sOut = sOut & s
        Next i
        sFilter = sOut
        '
    End Function
    
    Public Function sFilterNonAlphaNumeric(sString As String) As String
        ' Leaves upper and lower case in tact.
        Dim s As String
        s = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        sFilterNonAlphaNumeric = sFilter(sString, s)
    End Function
    
    Public Function sFilterNonPrintable(sString As String) As String
        Dim i As Long
        Static sAllowed As String
        '
        If Len(sAllowed) = 0 Then
            For i = 32 To 126
                sAllowed = sAllowed & Chr$(i)
            Next i
        End If
        sFilterNonPrintable = sFilter(sString, sAllowed)
    End Function
    
    
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    MN
    Posts
    362

    Re: [RESOLVED] find and list only letters/numbers in string

    is there an Insert keyword for vb6? Say i want to insert the letter 'T' at position 5 in a textbox. That textbox may already have something in it, like 'We won the football game.' I'd want the letter 'T' to be inserted at the 5th character -> 'We wTon the football game.'

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] find and list only letters/numbers in string

    is there an Insert keyword for vb6?
    Code:
    Text1.SelStart = n-1
    Text1.SelLength = 0
    Text1.SelText = new text
    Last edited by LaVolpe; Mar 20th, 2017 at 08:56 PM. Reason: added code tags
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width