strtrim - Remove leading and trailing whitespace from strings - MATLAB (original) (raw)
Main Content
Remove leading and trailing whitespace from strings
Syntax
Description
Note
strip is recommended overstrtrim
because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality.
newStr = strtrim([str](#bu8cvc2-txt))
removes leading and trailing whitespace characters from str
and returns the result as newStr
. However, strtrim
does not remove significant whitespace characters. For example,strtrim
removes leading and trailing space and tab characters, but does not remove the nonbreaking space character,char(160)
.
Examples
Create a character vector with spaces and a tab character as leading whitespace.
chr = sprintf(' \t Remove leading whitespace')
chr = ' Remove leading whitespace'
Remove the leading tab and spaces.
newChr = 'Remove leading whitespace'
strtrim
removes the leading whitespace characters, but not the whitespace between other characters.
Create a string array.
str = [" Gemini "," Apollo "; " ISS "," Skylab "]
str = 2×2 string " Gemini " " Apollo " " ISS " " Skylab "
Remove leading and trailing whitespace with the strtrim
function.
newStr = 2×2 string "Gemini" "Apollo" "ISS" "Skylab"
Remove the leading and trailing whitespace from all the character vectors in a cell array and display them.
chr = {' Trim leading whitespace'; 'Trim trailing whitespace '}
chr = 2×1 cell {' Trim leading whitespace' } {'Trim trailing whitespace '}
newChr = 2×1 cell {'Trim leading whitespace' } {'Trim trailing whitespace'}
Create a character vector that includes the nonbreaking space character, char(160)
, as a trailing whitespace character.
chr = ' Keep nonbreaking space'; chr = [chr char(160) ' '];
Display chr
between | symbols to show the leading and trailing whitespace.
ans = '| Keep nonbreaking space |'
Remove the leading and trailing whitespace characters.
Display newChr
between | symbols. strtrim
removes the space characters but leaves the nonbreaking space at the end of newChr
.
ans = '|Keep nonbreaking space |'
Input Arguments
Input text, specified as a character array or as a cell array of character arrays, or a string array.
Algorithms
strtrim
does not remove significant whitespace characters.
This table shows the most common characters that are significant whitespace characters and their descriptions. For more information, see Whitespace character.
Significant Whitespace Character | Description |
---|---|
char(133) | Next line |
char(160) | Nonbreaking space |
char(8199) | Figure space |
char(8239) | Narrow no-break space |
Alternative Functionality
Update code that makes use of strtrim
to use strip instead. For example:
Not Recommended | Recommended |
---|---|
str = " test "; newStr = strtrim(str) newStr = "test" | str = " test "; newStr = strip(str) newStr = "test" |
Extended Capabilities
Thestrtrim
function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
- Input text must be string scalar or a character array.
- Input values must be in the range 0–127.
- Generated code returns an empty output as a 1-by-0 character array.
Version History
Introduced before R2006a