String matching: Difference between revisions

GP
(GP)
Line 637:
that other cloud looks like a llama" "cloud" ;;
- : bool * int = (true, 2)
 
=={{header|PARI/GP}}==
This meets the first but not the second of the optional requirements. Note that GP treats any nonzero value as true so the location found by contains() can be ignore if not needed.
<lang parigp>startsWith(string, prefix)={
string=Vec(string);
prefix=Vec(prefix);
if(#prefix > #string, return(0));
for(i=1,#prefix,if(prefix[i]!=string[i], return(0)));
1
};
contains(string, inner)={
my(good);
string=Vec(string);
inner=Vec(inner);
for(i=0,#string-#inner,
good=1;
for(j=1,#inner,
if(inner[j]!=string[i+j], good=0; break)
);
if(good, return(i+1))
);
0
};
endsWith(string, suffix)={
string=Vec(string);
suffix=Vec(suffix);
if(#suffix > #string, return(0));
for(i=1,#suffix,if(prefix[i]!=string[i+#string-#suffix], return(0)));
1
};</lang>
 
=={{header|Perl}}==