Link to home
Start Free TrialLog in
Avatar of Cyber-EE
Cyber-EE

asked on

Find a string in another string

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.


ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaliyugkaarjun
kaliyugkaarjun

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
kaliyugkaarjun, the optional start argument is the first argument, not the third:

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

Jon
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
alainbryden's apprach is the easiest
-Dean
Hey bro just chk out i have written  comment  and not statement in vb :D

//substring found so do ur required operation

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

cheers
ya kalyugkaarjun ... ur comment was good but a bit "Java"ish ...


be happy
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])




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.