Main Content

strjust

Justify strings

Description

Note

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

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.

example

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.

example

Examples

collapse all

Create a string array in which some elements have trailing whitespace characters.

str1 = ["Skylab";
        "Mir   ";
        "ISS   "]
str1 = 3x1 string
    "Skylab"
    "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
    "Skylab"
    "   Mir"
    "   ISS"

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

C1 = {'Euler    ';
      'Fibonacci';
      '    Gauss'}
C1 = 3x1 cell
    {'Euler    '}
    {'Fibonacci'}
    {'    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  '}
    {'Fibonacci'}
    {'  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.

Alternative Functionality

Update code that makes use of strjust to use pad instead. If justifying text left or right, also use strip to remove whitespace from the text first. For example:

ContextNot RecommendedRecommended
Right justificationstrjust(A)pad(strip(A),"left")
Left justificationstrjust(A)pad(strip(A),"right")
Center justificationstrjust(A,"center")pad(A,"both")

Extended Capabilities

Version History

Introduced before R2006a