?
Solved

How to detect a word in a string!

Posted on 1999-07-28
6
Medium Priority
?
148 Views
Last Modified: 2011-04-14
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
Comment
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
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
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

by:pietjepuk
ID: 1527955
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
 
LVL 1

Expert Comment

by:pietjepuk
ID: 1527956
Oops,

I'm sorry caraf_g. I had the question opened before you answered it so I did not see your answer.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:caraf_g
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

by:pietjepuk
ID: 1527958
You are right, should be:

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

Thanks.
0
 
LVL 1

Author Comment

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

Thanks,

Cindy
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question