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

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
Asked:
CindyGraves
  • 3
  • 2
1 Solution
 
caraf_gCommented:
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
 
pietjepukCommented:
Dim strString1 As String
Dim strString2 As String

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

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

'Also check Functions Left, Mid and Instr
0
 
pietjepukCommented:
Oops,

I'm sorry caraf_g. I had the question opened before you answered it so I did not see your answer.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
caraf_gCommented:
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
 
pietjepukCommented:
You are right, should be:

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

Thanks.
0
 
CindyGravesAuthor Commented:
Thank you very much- It worked just great!!!!!!!

Thanks,

Cindy
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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