#### Archived

This topic is now archived and is closed to further replies.

This topic is 5995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay... I want to read in one five-digit Integer and determine how many digits in the Integer are 7''s. I was wondering if there is a VB function for this????

##### Share on other sites
Not really. But you can do this:

  Public Function count7(Num as integer) as integer Dim strNum as string, i as integer, cnt as integer strNum = Str(Num) For i = 1 to len(strNum) If mid(strNum, i, 1) = "7" then _ cnt = cnt + 1 Next i count7 = cntEnd Function

##### Share on other sites
Or even further, you can do this..

  Public Function count7(num as integer) as integer Dim strNum as string, i as integer, i2 as integer strNum = str(num) i = instr(strNum, "7") While Not (i=0) i2 = i i = instr(i+1, strNum, "7") Loop count7 = i2End Function

This will probably work faster.

Edited by - sporty on February 20, 2002 8:10:47 PM

##### Share on other sites
why not use do until instead of while not?

do until (i=0)

or even "while i" should work properly... since anything not = 0 SHOULD be true (I think VB handles this properly).

Also, i think Sporty messed up.. he put i2 = i, where he really meant i2 = i2 + 1

Billy - BillyB@mrsnj.com

I really should create a user name sometime

##### Share on other sites
Yes you''re right, I forgot that part

I only use While Not because I''m simply more used to reading it that way. Seems a bit clearer when I''m quickly browsing old code. Just a preference.

##### Share on other sites
I''m actually used to using while not also (as that''s what we get in C/C++ while loops), but in VB I prefer to use until as it makes it very easy to read.. do until x = 0... just as simple (if not simpler) to read as do while not (x=0)... but again, just personal preference.

Billy

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002288
×