Link to home
Start Free TrialLog in
Avatar of AmericaFan
AmericaFan

asked on

VB6 For Loop

I have a small routeen that removes all characters from a string but not any digit 0 thru 9. It works fine, but I was wondering if my For Next loop could be modified to be cleaner. Here is my code:

'*********************************************

For i = 0 To 47
MyString = Replace(MyString, Chr(i), "")
Next i

For i = 58 To 127
MyString = Replace(MyString, Chr(i), "")
Next i

'********************************************************
Avatar of aikimark
aikimark
Flag of United States of America image

For i = 0 To 47
  MyString = Replace(MyString, Chr(i), "")
Next i

1. Do you really need to concern yourself with characters less than 32?

2. Is cleaner really your problem, or are you experiencing performance problems?  Your two loops look pretty clean.  You combine these two loops, but wouldn't gain any simplicity or cleanliness.

3. How long are your MyString strings?
Avatar of AmericaFan
AmericaFan

ASKER

My main goal was to see if I could combine the two For Loops into one For Loop and get the same results. I only want 0-9 and any other character has to be removed. I'm validating and any key is possible. Maybe not 0 thru 31, but the rest is not wanted and is on a key board for mistakes. Thanks for you comment aikimark.
ASKER CERTIFIED SOLUTION
Avatar of Clif
Clif
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also use regular expressions, which is even more efficient.

You would have to add a reference to "Microsoft VBScript Regular Expressions 5.5" in your project (it's been available since IE 5.5)

Thats what I'm talkin about :) Thanks Clif!
NP
Thanks