Search code examples
prologwhitespacetrim

How to trim right whitespaces from String in prolog?


I would like to trim right whitespace from strings for example. "Dog " becomes "Dog" , " " becomes "". How could this be achieved?

I know SWI prolog has trim whitespace predicate however it trims String from right and left.

However I am also using GNU prolog so looking to write my own solution.


Solution

  • You can convert a string into a list of characters (see convert string to list in prolog), write a predicate to manipulate it however you want, and then convert the resulting list back to a string.