Documentation

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.

strtrim

Remove leading and trailing whitespace from character array

Syntax

Description

example

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

collapse all

Remove Leading Whitespace from Character Vector

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.

Remove Leading and Trailing Whitespace from Cell Array

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 = 

    '     Trim leading whitespace'
    'Trim trailing whitespace     '

newChr = strtrim(chr)
newChr = 

    'Trim leading whitespace'
    'Trim trailing whitespace'

Keep Nonbreaking Space Character

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

chr — Input textcharacter array | cell array of character arrays

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

Data Types: char | cell

More About

collapse all

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

See Also

| | |

Introduced before R2006a

Was this topic helpful?