GNU Prolog is one of the supported Logtalk backend compilers. With it, you gain access to (portable) libraries (and developer tools!) that allows you to easily solve the problem. For example, if your strings are represented using atoms, you can use the atom::replace_sub_atom/4
predicate:
| ?- {types(loader)}.
...
yes
| ?- atom::replace_sub_atom(' ', '', 'Dog ', Output).
Output = 'Dog'
yes
| ?- atom::replace_sub_atom(' ', '', ' becomes ', Output).
Output = becomes
If instead you're using double-quoted terms, their meaning depends on the double_quotes
flag value, In this case, you can use the list::subtract/3
predicate. For example:
| ?- current_prolog_flag(double_quotes, Value).
Value = codes
yes
yes
| ?- list::subtract("Dog ", " ", Output).
Output = [68,111,103]
yes
| ?- list::subtract(" becomes ", " ", Output).
Output = [98,101,99,111,109,101,115]
yes
| ?- set_prolog_flag(double_quotes, chars).
yes
| ?- list::subtract("Dog ", " ", Output).
Output = ['D',o,g]
yes
| ?- list::subtract(" becomes ", " ", Output).
Output = [b,e,c,o,m,e,s]
yes
| ?- set_prolog_flag(double_quotes, atom).
yes
| ?- atom::replace_sub_atom(" ", "", "Dog ", Output).
Output = 'Dog'
yes
| ?- atom::replace_sub_atom(" ", "", " becomes ", Output).
Output = becomes
yes