Solved

Find a string in another string

Posted on 2006-06-20
14
308 Views
Last Modified: 2010-08-05
What function can help me find a string in another string.
Example :

string str1 = "i am in my house"
string str2 = "in my"
string str3 = "inmy"

compare_function(str1,str2) will return true because str2 was found in str1.
compare_function(str1,str3) will return false.


0
Comment
Question by:Cyber-EE
[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
  • 3
  • 2
  • +2
14 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 50 total points
ID: 16941408
you can use the Instr function to accomplish this.  Also StrComp.  Look them both up in the VB help system.

(Give a man a fish and you feed him for a day.  Teach a man how to fish for himself, and you feed him for the rest of his life)

AW
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16942073
use Instr() function it returns the position of the first occurrence of one string within another.

InStr(sCheck, sMatch[, Start[, Compare]])

Arguments:
sCheck      => Required. String expression being searched.
sMatch      =>Required. String expression being searched for.
Start         Optional.  => Numeric expression that sets the starting position for each search. If omitted, search   begins at the first character position (Start = 1).
Compare      Optional.  => Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed.

Example:
InStr("abc", "a")             =>  1

For ur code :
dim i=0;
if Instr(str1,str2)  != 0 then
//substring found so do ur required operation
end if

Cheers
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 16942462
kaliyugkaarjun, the optional start argument is the first argument, not the third:

InStr([start, ]string1, string2[, compare])

Jon
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 20

Expert Comment

by:alainbryden
ID: 16943478
jmundsack is right. Arthur, is too, he should probably look up something so simple, but he did offer up points that he may or may not have payed for so he might as well get a full answer. To make Kaliyugkaarjun's answer more legible and correct:

Function compare_function(byVal str1 As String, str2 As String) As Boolean
    compare_function = InStr$(str1, str2) <> 0
End Function

(To Kaliyugkaarjun's: != does not make sense in visual basic and dim i = 0 was not necessary and 'ur' is not a word.)

Cheers :D

Alain
0
 

Expert Comment

by:deandany
ID: 16943993
alainbryden's apprach is the easiest
-Dean
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16948578
Hey bro just chk out i have written  comment  and not statement in vb :D

//substring found so do ur required operation

cheers
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 16951657
Hey bro, the proper way to comment in vb is with ' not //, this isn't java :D

cheers
0
 

Expert Comment

by:deandany
ID: 16952793
ya kalyugkaarjun ... ur comment was good but a bit "Java"ish ...


be happy
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16957217
Forget it ...it was just to mention that the line has nothing to do with code...

InStrRev (VB 6 Only)        

>Returns the first occurance of one string in another, starting from the right to the left.  

>InstrRev(string1, string2[,  start[, compare]])

InStr  

>Returns the first occurance of one string in another, starting from the left to the right.        

>InStr([start, ]string1, string2[, compare])




0
 
LVL 20

Expert Comment

by:alainbryden
ID: 17308209
I'd object, while Arthur_wood gave a partial answer, leading in the right direction, what the user wanted was a working solution, and it was not he, but kaliyugkaarjun (almost) and I that gave a working solution.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month4 days, 4 hours left to enroll

630 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