Solved

# How to detect a word in a string!

Posted on 1999-07-28
Medium Priority
148 Views
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
Question by:CindyGraves
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 10

Accepted Solution

caraf_g earned 200 total points
ID: 1527954
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

LVL 1

Expert Comment

ID: 1527955
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

LVL 1

Expert Comment

ID: 1527956
Oops,

0

LVL 10

Expert Comment

ID: 1527957
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

LVL 1

Expert Comment

ID: 1527958
You are right, should be:

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

Thanks.
0

LVL 1

Author Comment

ID: 1527959
Thank you very much- It worked just great!!!!!!!

Thanks,

Cindy
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This lâ€¦
###### Suggested Courses
Course of the Month10 days, 17 hours left to enroll