This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Justify string or character array


newTxt = strjust(txt)
newTxt = strjust(txt,side)



newTxt = strjust(txt) returns a right-justified version of the text in txt.

  • If txt has trailing whitespace characters, then they become leading whitespace characters in newTxt.

  • If txt does not have trailing whitespace characters, then strjust returns txt unaltered.


newTxt = strjust(txt,side) returns a version of the text that is justified on the side specified by side. The text in txt can be justified on the left, right, or center.


collapse all

Create a string array in which some elements have trailing whitespace characters. Starting in R2017a, you can create strings using double quotes.

str1 = ["Skylab";
        "Mir   ";
        "ISS   "]
str1 = 3x1 string array
    "Mir   "
    "ISS   "

Justify the text on the right. strjust does not alter the first element because "Skylab" has no whitespace.

str2 = strjust(str1)
str2 = 3x1 string array
    "   Mir"
    "   ISS"

Create a cell array of character vectors. Some of the character vectors have leading and trailing whitespace characters.

C1 = {'Euler    ';
      '    Gauss'}
C1 = 3x1 cell array
    {'Euler    '}
    {'    Gauss'}

Center-justify the text. If a piece of text has leading or trailing whitespace, or both, then strjust adjusts the text to have an equal number of leading and trailing whitespace characters. strjust does not alter a piece of text when it has neither leading nor trailing whitespace.

C2 = strjust(C1,'center')
C2 = 3x1 cell array
    {'  Euler  '}
    {'  Gauss  '}

Input Arguments

collapse all

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

Data Types: char | cell | string

Side to justify text on, specified as 'left', 'right', or 'center'. The default behavior is to justify the text on the right.

Data Types: char | string

Extended Capabilities

Introduced before R2006a