# Remove all substrings in a string

**URL:** <https://swi-prolog.discourse.group/t/remove-all-substrings-in-a-string/3280>\
**Category:** Algorithm\
**Created:** [November 19, 2020, 6:16am UTC](https://swi-prolog.discourse.group/t/remove-all-substrings-in-a-string/3280 "2020-11-19T06:16:49Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 20, 2020, 7:53am UTC](https://swi-prolog.discourse.group/t/remove-all-substrings-in-a-string/3280/7 "2020-11-20T07:53:47Z")

</div>

Never underestimate append/3:

```prolog
subst(This, That, MyStr, Result) :-
    append(This, After, Rest),
    append(Before, Rest, MyStr),
    !,
    subst(This, That, After, AfterResult),
    append([Before,That,AfterResult], Result).
subst(_, _, S, S).

```

To test:

```prolog
103 ?- portray_text(true).
true.

104 ?- subst(`this`,`that`,`athishellothis`,R).
R = `athathellothat`.

```

But yes, especially if you have atoms or strings to begin with and portability is not a goal, re\_replace/4 is probably a simpler and more efficient choice.

---

_[View the full topic](https://swi-prolog.discourse.group/t/remove-all-substrings-in-a-string/3280)._
