Results 1 to 6 of 6

Thread: [RESOLVED] Replacing Numbers?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Resolved [RESOLVED] Replacing Numbers?

    How do I replace numbers in a string with "#"?

    -Zach

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Replacing Numbers?

    NVM I'm smart and I got it.

    -Resolved-

  3. #3
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Replacing Numbers?

    Quote Originally Posted by Zach_VB6
    How do I replace numbers in a string with "#"?

    -Zach
    How about using the Replace function?
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] Replacing Numbers?

    I didn't know how to identify the numbers.

    I used:

    Code:
    Public Function RemoveNumbers(ByVal sString As String) As String
    Dim lLen As Long, sTemp As String, sFinal As String
    sFinal = sString
    
        For lLen = 1 To Len(sString)
            sTemp = Mid$(sString, lLen, 1)
                If IsNumeric(sTemp) Then sFinal = Replace(sFinal, sTemp, "#", , 1)
        Next
    RemoveNumbers = sFinal
    End Function

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Replacing Numbers?

    That would be slow. Does this need to be optimized? Are there speed considerations?

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Replacing Numbers?

    Quote Originally Posted by Zach_VB6
    I didn't know how to identify the numbers.
    The numbers (digits 0 thru 9) are character codes 48 thru 57.

    This is fast:
    Code:
    Private Function MaskNumbers(sString As String) As String
      Dim i As Long, lPos As Long, strChar As String
      
      MaskNumbers = sString
      For i = 48 To 57 ' Character codes for digits "0" thru "9"
        strChar = Chr$(i)
        lPos = InStr(1, MaskNumbers, strChar) ' Find first matching character
        Do Until lPos = 0
          Mid(MaskNumbers, lPos, 1) = "#" ' Change the character
          lPos = InStr(lPos + 1, MaskNumbers, strChar) ' Find next character
        Loop
      Next i
    End Function
    This is faster:
    Code:
    Private Function MaskNumbers(sString As String) As String
      Dim btChars() As Byte, i As Long
      
      btChars = sString ' Convert string to byte array
      For i = 0 To UBound(btChars) Step 2 ' Step 2 because it's unicode
        Select Case btChars(i) ' Check character code
        Case 48 To 57 ' If it's a digit
          ' It's only a digit if the high byte is zero
          If btChars(i + 1) = 0 Then btChars(i) = 35 ' Change the character code to that of "#"
        End Select
      Next i
      MaskNumbers = btChars ' Convert to string
    End Function

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