Strip trailing blanks from end of string


str = deblank(str)
c = deblank(c)


str = deblank(str) removes all trailing whitespace and null characters from the end of character string str. A whitespace is any character for which the isspace function returns logical 1 (true).

c = deblank(c) when c is a cell array of strings, applies deblank to each element of c.

The deblank function is useful for cleaning up the rows of a character array.


Example 1 – Removing Trailing Blanks From a String

Compose a string str that contains space, tab, and null characters:

NL = char(0);    TAB = char(9);
str = [NL 32 TAB NL 'AB' 32 NL 'CD' NL 32 TAB NL 32];

Display all characters of the string between | symbols:

['|' str '|']
ans =
    |    AB  CD       |

Remove trailing whitespace and null characters, and redisplay the string:

newstr = deblank(str);

['|' newstr '|']
ans =
    |    AB  CD|

Example 2– Removing Trailing Blanks From a Cell Array of Strings

Create a 2-by-2 cell array in which each cell contains a word with trailing blanks:

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

Remove the trailing blanks and redisplay the cell array:

A = deblank(A);

A = 
    'MATLAB'       'SIMULINK'
    'Toolboxes'    'MathWorks'

See Also


Introduced before R2006a

Was this topic helpful?