Main Content


Remove trailing whitespace from ends of strings




strip is recommended over deblank because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality.

newStr = deblank(str) removes trailing whitespace and null characters from str and returns the result as newStr. However, deblank does not remove significant whitespace characters. For example, deblank removes trailing space and tab characters, but does not remove the nonbreaking space character, char(160).


collapse all

Create a character vector that contains space, tab, and null characters.

chr = sprintf(' \t MathWorks \t');
chr = [chr char(0)];

Display chr between | characters.

['|' chr '|']
ans = 
'| 	 MathWorks 	 |'

Remove the trailing blanks and display newChr between | characters.

newChr = deblank(chr);
['|' newChr '|']
ans = 
'| 	 MathWorks|'

Create a character array that contains multiple pieces of text. When a character array contains multiple rows, it is often necessary to pad the array with space characters.

chr = ['Mercury';
       'Apollo ';
       'ISS    ']
chr = 3x7 char array
    'Apollo '
    'ISS    '

Convert chr to a string array. The elements of str include the trailing space characters that padded chr.

str = string(chr)
str = 3x1 string
    "Apollo "
    "ISS    "

To remove the trailing spaces, use the deblank function.

newStr = deblank(str)
newStr = 3x1 string

Remove trailing blanks from all the character vectors in a cell array and display them.

A = {'MATLAB    ','SIMULINK    ';
     'Toolboxes    ','MathWorks    '}
A = 2x2 cell
    {'MATLAB    '   }    {'SIMULINK    ' }
    {'Toolboxes    '}    {'MathWorks    '}

B = deblank(A)
B = 2x2 cell
    {'MATLAB'   }    {'SIMULINK' }
    {'Toolboxes'}    {'MathWorks'}

Create a character vector that includes the nonbreaking space character, char(160), as a trailing blank character.

chr = '     MathWorks';
chr = [chr char(160) '     '];

Display chr between | symbols to show the leading and trailing whitespace.

['|' chr '|']
ans = 
'|     MathWorks      |'

Remove the trailing whitespace characters.

newChr = deblank(chr);

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

['|' newChr '|']
ans = 
'|     MathWorks |'

Input Arguments

collapse all

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


deblank 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

Alternative Functionality

Update code that makes use of deblank to use strip instead. Unlike deblank, which only removes whitespace characters from the end of text, strip removes both leading and trailing whitespace characters. To remove only trailing whitespace characters using strip, specify the "right" option. For example:

Not RecommendedRecommended
str = "     test  ";
newStr = deblank(str)
newStr = 

    "     test"
str = "     test  ";
newStr = strip(str,"right")
newStr = 

    "     test"

Extended Capabilities

Version History

Introduced before R2006a