Sub is sub_string( String, Start, Length )
End is string_end( String, Start, Length )
sub_string( Sub, String, Start, Length )
string_end( End, String, Start, Length )
sub_string_no( Sub, String, Start, Length )
string_end_no( End, String, Start, Length )
The function sub_string returns the substring of String that starts from position Start and has length Length. The variable String has to be instantiated with a string, other variables can be not-instantiated. So, in the general case the function sub_string returns many answers.
The function string_end is the same as sub_string with that difference that it returns only these substrings which are ends.
The predicates are the same as the functions with that difference that you can use them
to search for a concrete substring (when the argument Sub (End) is instantiated
with a concrete substring). If you want to search without case use the modifications
sub_string_no and string_end_no.
By putting data in some of the variables Sub , Start and Length and leaving the others not-instantiated, you can use the predicate sub_string in six various ways:
1. If all variables are instantiated then it checks if the string Sub is substring of String which starts from Start and having length Length . If in this case Length is not instantiated then it receives the length of Sub .
2. If only Sub is instantiated then predicate works as Find . It search for occurrence of Sub in String . Once found, it returns its start position in Start . By backtracking this predicate returns all such start positions (the Sub can occur several times in String ). In Length this predicate returns the length of Sub . If Length is instantiated then checks if the length of Sub is equal to Length .
3. If only Sub is not instantiated then instantiates it with the substring of String which starts from Start and having length Length .
4. If Sub , Start and Length are not-instantiated then returns in them a substring with its start position and length. By backtracking this predicate returns all substrings of String with their start positions and lengths (look at the example).
5. If Sub and Start are not-instantiated, but Length is instantiated then returns in Sub and Start a substring with its start position which has length Length . By backtracking this predicate returns all substrings of String which have length Length .
6. If Sub and Length are not-instantiated, but Start is instantiated then returns in Sub and Length a substring with its length which has start position Start . By backtracking this predicate returns all substrings of String which have start position Start .
?- sub_string(Sub, "Sub String",
Start, Length), write([Sub, Start, Length]), nl, fail.