Main Content


Justify strings



newStr = strjust(str) returns a right-justified version of the text in str.

  • If str has trailing whitespace characters, then they become leading whitespace characters in newStr.

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


newStr = strjust(str,side) returns a version of the text that is justified on the side specified by side. The text in str 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
    "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
    "   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
    {'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
    {'  Euler  '}
    {'  Gauss  '}

Input Arguments

collapse all

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

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

Extended Capabilities

Introduced before R2006a