Welcome to Dream.In.Code
Become a VB Expert!

Join 149,545 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,561 people online right now. Registration is fast and FREE... Join Now!




Efficiently scanning strings for numeric characters

 
Reply to this topicStart new topic

Efficiently scanning strings for numeric characters

Darxide
18 Nov, 2007 - 08:13 AM
Post #1

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 3


My Contributions
I need a little help making this code as efficient as I can. Whenever I try to do it it gets all long and complicated and I'm sure it doesn't need to be.

I will have a string input (a filename actually) and I need to scan it for any numeric characters. The idea is that one of the numbers will increment and the others will remain static. The problem is that I never know just how many sets of numeric characters there will be. Assuming there were no static numbers I wouldn't be posting here at all. A better example I can think of would be "Area 51 - page 3". Without explanation, you could see that "51" is the static number that should remain the same across all filenames and that the "3" is the incrementing number. The incrementing number will not always be at the end of the string, either. I will be preforming string comparisons later to determine which number is incrementing. I can do that part on my own. There may be instances where there are many more than two sets of numeric characters in the name, however. It's an unknown.

So far, what I've come up with has been something like this:

CODE

******  PSUEDOCODE  ******

dim sFilename as string      'the filename to scan
dim iNumcount as integer   'holds how many sets of numeric characters I encounter
dim x, y as integer
dim s as string

y = 0

for x = 0 to strlen(sFilename)

if y = 0 then
s = mid(sFilename, x)
if s = is_a_number then y = 1   'y lets me know we're inside of a numeric set (ie: 51 counts as one number, not two)
iNumcount = iNumcount + 1
else  'y = 1
s = mid(sFilename, x)
if s = is__not_a_number then y = 0
endif

next


From there it gets overly complicated with getting each set of numeric characters out with for..next loops and I usually break down and start over. I've been going over this for a couple of weeks now and finally decided to ask for help.

I know I'm going about it all wrong, so if someone wants to help me out with a push in the right direction, I would very much appreciate it.

Thanks,
~Darxide
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Efficiently Scanning Strings For Numeric Characters
18 Nov, 2007 - 12:31 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Have you ever considered using regular expressions for this? I am not sure if you are using .NET or VB 6, but both support the use of regular expressions (.NET has it built in and VB 6 needs the vbscript library).

The idea with regular expressions is that it look for patterns and creates matches. For instance... If I had the string "Area 51 - Page 3" I could throw a regular expression of [0-9]+ against it and it would result in an array of matches. This array would have two elements... one match would be "51" and the second would be "3".

Then once you have your matches, all you would have to do is compare element 2 (the number 3) with element 2 of another string (lets say it was 4) and you would know which match is changing.

To give you an idea of how this works, you can try the following website...

Regular Expression Library

At the bottom you will see two boxes, the first will say "Source" and the second will say "Pattern". In the source box type Area 51 - Page 3 and in the pattern box type [0-9]+ and hits submit. Then scroll down to the bottom and you will see a place where it says "Matches" and shows you the two numbers from the string.

Once you try this out, I am sure your problems will dissolve like button on a hot skillet.

Let us know if you have any further questions.

"At DIC we be regular expression loving code ninjas!" decap.gif

This post has been edited by Martyr2: 18 Nov, 2007 - 12:32 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month