Rebol3 Code Examplex


Strip whitespace from a string/Top and tail

Remove leading and trailing whitespace from a string while preserving the text in the middle.

Rebol [
    title: "Rosetta code: Strip whitespace from a string/Top and tail"
    file:  %Strip_whitespace_from_a_string-Top_and_tail.r3
    url:   https://rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tail
]

str: "     Hello World     "

print [pad "strip leading:"  15 mold trim/head      copy str]
print [pad "strip trailing:" 15 mold trim/tail      copy str]
print [pad "strip all:"      15 mold trim/head/tail copy str]

Output:

strip leading:  "Hello World     "
strip trailing: "     Hello World"
strip all:      "Hello World"