|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Check DigitsI have a program that calculates some numbers. Now, it there a easy way of
determine if any of the digits in the result is not repeated? For example, if my result was 156285640, I would like to know that number 6 is twice in the result. Any help would be appreciated. Perhaps something like:
Dim strX As String Dim strDigit As String strDigit = "6" strX = "156285640" MsgBox strDigit & " occurs " & _ Len(strX) - Len(Replace(strX, strDigit, "")) & _ " times." Good luck! Saga Show quoteHide quote "HLong" <HL***@discussions.microsoft.com> wrote in message news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com... >I have a program that calculates some numbers. Now, it there a easy >way of > determine if any of the digits in the result is not repeated? For > example, if > my result was 156285640, I would like to know that number 6 is twice > in the > result. Any help would be appreciated. Saga wrote:
Show quoteHide quote > For the more general case, convert to string w/ Format$(), take each> Perhaps something like: > > Dim strX As String > Dim strDigit As String > > strDigit = "6" > strX = "156285640" > > MsgBox strDigit & " occurs " & _ > Len(strX) - Len(Replace(strX, strDigit, "")) & _ > " times." > > Good luck! > Saga > > "HLong" <HL***@discussions.microsoft.com> wrote in message > news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com... > >I have a program that calculates some numbers. Now, it there a easy > >way of > > determine if any of the digits in the result is not repeated? For > > example, if > > my result was 156285640, I would like to know that number 6 is twice > > in the > > result. Any help would be appreciated. character and sort them, then look for repeated sequences. > "HLong" <HL***@discussions.microsoft.com> wrote in message Here's something I just threw together on the quick. Just adding to the> news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com... > >I have a program that calculates some numbers. Now, it there a easy > >way of > > determine if any of the digits in the result is not repeated? For > > example, if > > my result was 156285640, I would like to know that number 6 is twice > > in the > > result. Any help would be appreciated. stew <g> Private Function HowMany(ByVal Numb As String, ByVal Digit As String) As Integer Dim iPos As Long, hPos As Long hPos = 1 Do iPos = InStr(hPos, Numb, Digit) If iPos > 0 Then hPos = iPos + 1 HowMany = HowMany + 1 End If Loop Until iPos = 0 End Function Thanks to all. I used your function and it worked great.
Show quoteHide quote "BeastFish" wrote: > > "HLong" <HL***@discussions.microsoft.com> wrote in message > > news:73DB02F9-D488-4CAF-9804-9483D7854C91@microsoft.com... > > >I have a program that calculates some numbers. Now, it there a easy > > >way of > > > determine if any of the digits in the result is not repeated? For > > > example, if > > > my result was 156285640, I would like to know that number 6 is twice > > > in the > > > result. Any help would be appreciated. > > > Here's something I just threw together on the quick. Just adding to the > stew <g> > > Private Function HowMany(ByVal Numb As String, ByVal Digit As String) As > Integer > Dim iPos As Long, hPos As Long > hPos = 1 > Do > iPos = InStr(hPos, Numb, Digit) > If iPos > 0 Then > hPos = iPos + 1 > HowMany = HowMany + 1 > End If > Loop Until iPos = 0 > End Function > > > > <snip>
Show quoteHide quote > Here's something I just threw together on the quick. Just adding to the How about a good old one-liner:> stew <g> > > Private Function HowMany(ByVal Numb As String, ByVal Digit As String) As > Integer > Dim iPos As Long, hPos As Long > hPos = 1 > Do > iPos = InStr(hPos, Numb, Digit) > If iPos > 0 Then > hPos = iPos + 1 > HowMany = HowMany + 1 > End If > Loop Until iPos = 0 > End Function > > > Private Sub Form_Load() Dim sNum As String Dim sDig As String sNum = "123456234634257625312354" sDig = "2" MsgBox HowMany(sNum, sDig) End Sub Private Function HowMany(ByVal sNumber As String, _ ByVal sDigit As String) _ As Integer HowMany = UBound(Split(sNumber, sDigit)) End Function Jim Edgar "HLong" <HL***@discussions.microsoft.com> wrote See if this might be useful:> I have a program that calculates some numbers. Now, it there a easy way of > determine if any of the digits in the result is not repeated? For example, if > my result was 156285640, I would like to know that number 6 is twice in the > result. Any help would be appreciated. HTH LFS Private Sub CheckDigits(ByRef Text As String) Dim Count(0 To 255) As Integer Dim idx As Long, ch As Long For idx = 1 To Len(Text) ch = Asc(Mid$(Text, idx, 1)) Count(ch) = Count(ch) + 1 Next For idx = "0" To "9" Debug.Print idx, Count(idx) Next End Sub It fails if there's a DBCS in the text Larry :-)
Tony Proctor Show quoteHide quote "Larry Serflaten" <serfla***@usinternet.com> wrote in message news:uTzlQq9ZFHA.3132@TK2MSFTNGP09.phx.gbl... > > "HLong" <HL***@discussions.microsoft.com> wrote > > I have a program that calculates some numbers. Now, it there a easy way of > > determine if any of the digits in the result is not repeated? For example, if > > my result was 156285640, I would like to know that number 6 is twice in the > > result. Any help would be appreciated. > > See if this might be useful: > > HTH > LFS > > Private Sub CheckDigits(ByRef Text As String) > Dim Count(0 To 255) As Integer > Dim idx As Long, ch As Long > > For idx = 1 To Len(Text) > ch = Asc(Mid$(Text, idx, 1)) > Count(ch) = Count(ch) + 1 > Next > > For idx = "0" To "9" > Debug.Print idx, Count(idx) > Next > > End Sub >
Code to INVERT image in VB6
How to find 3rd tuesday of the month simple but not clicking now Privileges and killing a process How do you code Asynchronous MP3 playing with VB6/API? Slow WMI Select Query VB6 and SPLIT Monitoring a software installation..... Tabulating document proerties from a folder in Access Sorting a 2d array by more than 1 column |
|||||||||||||||||||||||