Is there a way to check if a string is a substring of another string in Prolog? I tried converting the string to a list of chars and subsequently checking if the first set is a subset of the second that that doesn't seem to be restrictive enough. This is my current code:
isSubstring(X,Y):-
stringToLower(X,XLower),
stringToLower(Y,YLower),
isSubset(XLower,YLower).
isSubset([],_).
isSubset([H|T],Y):-
member(H,Y),
select(H,Y,Z),
isSubset(T,Z).
stringToLower([],[]).
stringToLower([Char1|Rest1],[Char2|Rest2]):-
char_type(Char2,to_lower(Char1)),
stringToLower(Rest1,Rest2).
If I test this with
isSubstring("test","tesZting").
it returns yes, but should return no.
The problem is with your
isSubset/2
.There are two distinct situations that you've tried to capture in one predicate. Either you're looking for the first position to try to match your substring, or you've already found that point and are checking whether the strings 'line up'.
You can combine these into one predicate, as follows:
Using DCG's you can do the following: (SWI)
Prolog strings are lists, where each element of the list is the integer value representing the codepoint of the character in question. The string
"abc"
is exactly equivalent to the list[97,98,99]
(assuming your prolog implementation is using Unicode or ASCII, otherwise the values might differ). That leads to this (probably suboptimal from a Big-O perspective) solution, which basically says that X is a substring of S ifHere's the code:
We restrict X to being something other than the empty list (aka the nil string
""
), since one could conceptually find an awful lot of zero-length substrings in any string: a string of length n has 2+(n-1) nil substrings, one between each character in the string, one preceding the first character and one following the last character.It is not clear what you mean by a string. But since you say you are converting it to a list, you could mean atoms. ISO Prolog offers
atom_concat/3
andsub_atom/5
for this purpose.Otherwise, use DCGs! Here's how
Acknowledgements
The first appearance of above definition of
...
is on p. 205, Note 1 of