?
Solved

VB6 For Loop

Posted on 2007-03-22
6
Medium Priority
?
39,429 Views
Last Modified: 2008-02-01
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

'********************************************************
0
Comment
Question by:AmericaFan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 18773570
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?
0
 

Author Comment

by:AmericaFan
ID: 18773708
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.
0
 
LVL 10

Accepted Solution

by:
Clif earned 200 total points
ID: 18773750
For i = 58 To 127
    MyString = Replace(MyString, Chr(i), "")
    If (i - 58) <= 47 Then
        MyString = Replace(MyString, Chr(i - 58), "")
    End If
Next i
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Clif
ID: 18773997
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)

0
 

Author Comment

by:AmericaFan
ID: 18774165
Thats what I'm talkin about :) Thanks Clif!
0
 
LVL 10

Expert Comment

by:Clif
ID: 18774253
NP
Thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question