strip

Removes leading and trailing characters from text.

Category: Language Fundamentals > Data Types

Syntax

s = strip(str)

Removes leading and trailing whitespace.

s = strip(str, side)

Strips only from the 'left', the 'right', or 'both' sides.

s = strip(str, side, chr)

Strips the character chr instead of whitespace.

Description

strip is similar to strtrim, but it can limit the operation to one side and can remove any chosen character, not only whitespace. Characters in the middle of the text are never touched.

Use the three-argument form for the character: strip('--x--', 'both', '-') gives 'x'.

Input arguments

str
String array, character vector, or cell array of character vectors.
side
'both' (default), 'left' or 'right'.
chr
A single character to remove; repeated occurrences at the edge are all removed.

Output arguments

s
Text of the same type and size as str, without the stripped characters.

Example

Whitespace and chosen characters

strip("  hi  ")
strip('--x--', 'both', '-')
strip('..a..', 'left', '.')
strip("  a  ", 'right')
Output
ans = "hi"
ans = 'x'
ans = 'a..'
ans = "  a"

Notes

The short form strip(str, chr) without a side is not handled correctly by MatLivre (the character is ignored and only whitespace is removed); pass the side explicitly, as in strip(str, 'both', chr).

See also

strtrim deblank erase replace