This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Remove leading and trailing whitespace from string array or character array




newTxt = strtrim(txt) removes leading and trailing whitespace characters from txt and returns the result as newTxt. 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).


collapse all

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 = strtrim(chr)
newChr =

Remove    leading whitespace

strtrim removes the leading whitespace characters, but not the whitespace between other characters.

Starting in R2016b, you can create string arrays with the string function, and remove leading and trailing whitespace with the strtrim function.

str = string({'   Gemini    ','   Apollo    ';
              '   ISS       ','   Skylab    '})
str = 

  2×2 string array

    "   Gemini    "    "   Apollo    "
    "   ISS       "    "   Skylab    "

newStr = strtrim(str)
newStr = 

  2×2 string array

    "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 array

    '     Trim leading whitespace'
    'Trim trailing whitespace     '

newChr = strtrim(chr)
newChr =

  2×1 cell array

    '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.

['|' chr '|']
ans =

|     Keep nonbreaking space      |

Remove the leading and trailing whitespace characters.

newChr = strtrim(chr);

Display newChr between | symbols. strtrim removes the space characters but leaves the nonbreaking space at the end of newChr.

['|' newChr '|']
ans =

|Keep nonbreaking space |

Input Arguments

collapse all

Input text, specified as a character array or as a cell array of character arrays, or a string array.

Data Types: char | cell | string

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.


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



Next line


Nonbreaking space


Figure space


Narrow no-break space

See Also

| | |

Introduced before R2006a

Was this topic helpful?