Main Content

pad

Add leading or trailing characters to strings

Description

newStr = pad(str) adds space characters to the ends of the strings in str, except for the longest one.

  • If str is a string array or cell array of character vectors with multiple elements, then pad adds space characters. All of the strings in newStr are as long as the longest element in str.

  • If str is a character vector, or a string array or cell array of character vectors with one element, then pad returns str unaltered.

example

newStr = pad(str,numberOfCharacters) adds space characters so the strings in newStr have the length specified by numberOfCharacters. If any strings in str have more characters than numberOfCharacters, then pad does not modify them.

example

newStr = pad(str,side) adds space characters to the side specified by side. The side argument can be 'left', 'right', or 'both'.

example

newStr = pad(str,numberOfCharacters,side) adds space characters to the side specified by side, up to the length specified by numberOfCharacters.

newStr = pad(___,padCharacter) pads strings with the character specified by padCharacter instead of the space character. You can use any of the input arguments in the previous syntaxes.

If str contains only one piece of text, then pad(str,padCharacter) returns str unaltered.

example

Examples

collapse all

Create a string array.

str = ["Mercury","Gemini","Apollo";
       "Skylab","Skylab B","ISS"]
str = 2x3 string
    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS"   

Pad the elements of str with space characters.

newStr = pad(str)
newStr = 2x3 string
    "Mercury "    "Gemini  "    "Apollo  "
    "Skylab  "    "Skylab B"    "ISS     "

Create a string array.

str = ["Mercury","Gemini","Apollo";
       "Skylab","Skylab B","ISS"]
str = 2x3 string
    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS"   

Specify the length so that even the longest string is padded with spaces.

newStr = pad(str,12)
newStr = 2x3 string
    "Mercury     "    "Gemini      "    "Apollo      "
    "Skylab      "    "Skylab B    "    "ISS         "

Create a string array.

str = ["Mary";"Elizabeth";"James"]
str = 3x1 string
    "Mary"
    "Elizabeth"
    "James"

Pad the strings to the left.

newStr = pad(str,'left')
newStr = 3x1 string
    "     Mary"
    "Elizabeth"
    "    James"

Pad both sides.

newStr = pad(str,'both')
newStr = 3x1 string
    "  Mary   "
    "Elizabeth"
    "  James  "

Create a string array representing numbers and pad the strings with leading zeroes instead of space characters.

A = [69.45 31.71 95.36 3.44 7.82];
A = A';
str = string(A)
str = 5x1 string
    "69.45"
    "31.71"
    "95.36"
    "3.44"
    "7.82"

newStr = pad(str,7,'left','0')
newStr = 5x1 string
    "0069.45"
    "0031.71"
    "0095.36"
    "0003.44"
    "0007.82"

Input Arguments

collapse all

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

Data Types: string | char | cell

Total number of characters in output strings, specified as a positive integer.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Side of string to pad, specified as 'left', 'right', or 'both'. The default behavior is to pad the right side of the string.

Data Types: char | string

Character to pad with, specified as a character or as a string that contains one character.

Data Types: char | string

Output Arguments

collapse all

Output text, returned as a string array, a character vector, or a cell array of character vectors. str and newStr are the same data type.

Data Types: string | char | cell

Extended Capabilities

Version History

Introduced in R2016b