In order to replace a substring in a string, I wrote a predicate called replace_substring
. It uses SWI-Prolog's append/3 predicate:
:- initialization(main).
:- set_prolog_flag(double_quotes, chars).
main :-
replace_substring("this is a string","string","replaced string",Result),
writeln(Result).
replace_substring(String,To_Replace,Replace_With,Result) :-
append(First,To_Replace,String),
append(First,Replace_With,Result).
Still, I'm not sure if this is the most efficient way to replace substrings in Prolog. Does Prolog have a built-in predicate that could be used for the same purpose?