strtrim

Removes leading and trailing whitespace from text.

Category: Language Fundamentals > Data Types

Syntax

s = strtrim(str)

Returns str without leading or trailing whitespace.

Description

Whitespace here means blanks, tabs, newlines, carriage returns and similar characters. Spaces inside the text are untouched. For a cell array, each element is trimmed on its own.

A character matrix is only trimmed of columns that are blank in every row, so rows stay aligned. To remove other characters from the edges, use strip.

Input arguments

str
Character vector, string array, or cell array of character vectors.

Output arguments

s
Text of the same type and size as str.

Example

Characters, cells and strings

strtrim(sprintf('  hi \t\n'))
strtrim({' a ', 'b  '})
strtrim("  two words  ")
Output
ans = 'hi'
ans = 1×2 cell

    {'a'}    {'b'}
ans = "two words"

See also

strip deblank pad regexprep