Anonymous user
String matching: Difference between revisions
→{{header|Prolog}}
Line 3,251:
=={{header|Prolog}}==
<lang prolog>
:- system:set_prolog_flag(double_quotes,codes) .
:- [library(lists)] .
%! starts_with(FIRSTz,SECONDz)
%
% True if `SECONDz` is the beginning of `FIRSTz` .
starts_with(FIRSTz,SECONDz)
:-
lists:append(SECONDz,_,FIRSTz)
.
%! contains(FIRSTz,SECONDz)
%
% True once if `SECONDz` is contained within `FIRSTz` at one or more positions .
contains(FIRSTz,SECONDz)
:-
contains(FIRSTz,SECONDz,_) ,
!
.
%! contains(FIRSTz,SECONDz,NTH1)
%
% True if `SECONDz` is contained within `FIRSTz` at position `NTH1` .
contains(FIRSTz,SECONDz,NTH1)
:-
lists:append([PREFIXz,SECONDz,_SUFFIXz_],FIRSTz) ,
prolog:length(PREFIXz,NTH0) ,
NTH1 is NTH0 + 1
.
%! ends_with(FIRSTz,SECONDz)
%
% True if `SECONDz` is the ending of `FIRSTz` .
ends_with(FIRSTz,SECONDz)
:-
lists:append(_,SECONDz,FIRSTz)
.
</lang>
{{out}}
<pre>
?- starts_with("abcdef","abc") .
true .
?- starts_with("abc","abc") .
true .
?- starts_with("abc","abcd") .
false .
?- starts_with("dabc","abc") .
false .
?- starts_with("","") .
true .
?-
?- contains("abcdef","abc") .
true.
?- contains("abcdef","abc",NTH).
NTH = 1 ;
false.
?- contains("abcdef","de",NTH).
NTH = 4 ;
false.
?- contains("abcdef","f",NTH).
NTH = 6 ;
false.
?- contains("abcde","f",NTH).
false.
?- contains("","",NTH).
NTH = 1 ; % wtf ?
false.
?- contains("a","a",NTH).
NTH = 1 ; % wtf ?
false.
?-
?- ends_with("abc","abc") .
true ;
false .
?- ends_with("abc","bc") .
true ;
false .
?- ends_with("abcd","bc") .
false .
?- ends_with("","") .
true ;
false .
?-
</pre>
=={{header|PureBasic}}==
<lang PureBasic>Procedure StartsWith(String1$, String2$)
|