|
-
Jun 15th, 2008, 11:31 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Replacing Numbers?
How do I replace numbers in a string with "#"?
-Zach
-
Jun 15th, 2008, 11:38 PM
#2
Thread Starter
Frenzied Member
Re: Replacing Numbers?
NVM I'm smart and I got it.
-Resolved-
-
Jun 15th, 2008, 11:42 PM
#3
Re: Replacing Numbers?
 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?? 
-
Jun 16th, 2008, 01:09 AM
#4
Thread Starter
Frenzied Member
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
-
Jun 16th, 2008, 01:13 AM
#5
Re: [RESOLVED] Replacing Numbers?
That would be slow. Does this need to be optimized? Are there speed considerations?
-
Jun 16th, 2008, 05:40 PM
#6
Re: [RESOLVED] Replacing Numbers?
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|