• Status: Solved
• Priority: Medium
• Security: Public
• Views: 150

# How to detect a word in a string!

I was wondering how I can detect a certain word in a string!-  Lets say that the string was heriexam.exe  How would I detect if the ending .exe is in there without mistaking the letters in heriexam for what I want-  What would be the full proof way of doing this!
0
CindyGraves
• 3
• 2
1 Solution

Commented:
Dim intPos As Integer

intPos = InStr(strYourString, ".exe")
If intPos > 0 Then
'The string was found
Do While intPos < len(strYourString) - 4
'Just in case your string happens to look like
'"Blah.exeBlahBlah.exe"
intPos = InStr(intPos + 1, strYourString, ".exe")
Loop
If intPos = len(strYourString) - 4 Then
'Hurray! it ends in ".exe"
End If
End If

0

Commented:
Dim strString1 As String
Dim strString2 As String

strString1 = "heriexam.exe"
strString2 = ".exe"

If Right(strString1, 4) = strString2 Then
MsgBox "Found"
Else
End If

'Also check Functions Left, Mid and Instr
0

Commented:
Oops,

0

Commented:
pietjepuk, your answer will fail if the string happens to be shorter than 4 characters.

But building on the idea:

If len(strYourString) < 4 Then
'Forget It
Else
If Right(strYourString, 4) = ".exe" Then
'hurray
End If
End If

0

Commented:
You are right, should be:

If Right(strString1, len(strString2)) = strString2 Then
MsgBox "Found"
Else
End If

Thanks.
0

Author Commented:
Thank you very much- It worked just great!!!!!!!

Thanks,

Cindy
0

## Featured Post

• 3
• 2
Tackle projects and never again get stuck behind a technical roadblock.