|
-
Aug 3rd, 2000, 02:47 AM
#1
Thread Starter
Fanatic Member
Howdy,
I would like to use one of the functions such as len,mid,instr to find out the exact number of specific letters in a string.
lets say the string is "Hello, take me to your leader".
and I want to find out how many of the letters "h","t" and "a" that are in this string.
the code I tryed to use is this.
Dim M As Integer
M = 1
For M = 1 To Len(textString)
If Mid(letters, M, 1) = "h" or "t" or "a" Then
intnumletters = intnumletters + 1
End If
Next M
NumOfMyletters = intnumletters
but this gives me a type mismatch error.
if I only try to use one letter(say "h") then it works fine but when I use Or it no longer works.
any help or suggestion would be great.
thank you
-
Aug 3rd, 2000, 04:00 AM
#2
New Member
If (Mid(letters, M, 1) = "h" or Mid(letters, M, 1) = "t" or Mid(letters, M, 1)) = "a" Then
-
Aug 3rd, 2000, 04:23 AM
#3
transcendental analytic
Using Instr
Code:
Dim n As Integer, a As Integer, match As String, textstring As String, numofmyletters As Integer
textstring = "ödahtahtaslästhat": match = "hta"
For n = 1 To Len(match)
a = InStr(textstring, Mid(match, n, 1))
Do While a
numofmyletters = numofmyletters + 1
a = InStr(a + 1, textstring, Mid(match, n, 1))
Loop
Next n
MsgBox numofmyletters
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 3rd, 2000, 09:52 AM
#4
Thread Starter
Fanatic Member
thanks
thanks for the help. I used your code ledger, that is kind of what I was trying to do but I geuss I had the syntax wrong.
Kedaman, I like your code BUT I don't understand it yet so I will be studying it for future use.
thanks to both of you.
-
Aug 3rd, 2000, 10:10 AM
#5
I like Kedaman's method, but you can clean it up a bit by placing it in a function and naming variables properly.
Code:
Function Match(sIn As String, sPattern As String) As Integer
Dim iPos As Integer
Dim iLetterCount As Integer
For I = 1 To Len(sPattern)
iPos = InStr(sIn, Mid(sPattern, I, 1))
Do While iPos
iLetterCount = iLetterCount + 1
iPos = InStr(iPos + 1, sIn, Mid(sPattern, I, 1))
Loop
Next I
Match = iLetterCount
End Function
Private Sub Command1_Click()
'USAGE: Match("String to search", "Pattern")
Retval = Match("Hello, take me to your leader", "hta")
MsgBox (Retval)
End Sub
-
Aug 3rd, 2000, 02:05 PM
#6
transcendental analytic
Sorry for not being present,m
AS you know INSTR searches for the first character in a string and returns the position, if you specify the start position, it will continue to search from that point. By searching from a + 1, you will avoid to find the same character over&over again. Also Instr will return 0 if there are no more characters found, so it will exit the loop on 0.
OK?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|