pad

Add leading or trailing characters to strings

Syntax

  • newStr = pad(str)
    example
  • newStr = pad(str,length)
    example
  • newStr = pad(str,side)
    example
  • newStr = pad(str,length,side)
  • newStr = pad(___,padCharacter)
    example

Description

example

newStr = pad(str) adds space characters to the end of each string in str. All of the strings in newStr are as long as the longest string in str.

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

example

newStr = pad(str,length) adds space characters to the end of each string, up to the length specified by length. If any string in str is longer than length, then pad does not modify that string.

example

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

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

example

newStr = pad(___,padCharacter) pads each string 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.

Examples

collapse all

str = string({'Mercury','Gemini','Apollo';...
              'Skylab','Skylab B','ISS'})
str = 

  2×3 string array

    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS"   

newStr = pad(str)
newStr = 

  2×3 string array

    "Mercury "    "Gemini  "    "Apollo  "
    "Skylab  "    "Skylab B"    "ISS     "

Create a string array.

str = string({'Mercury','Gemini','Apollo';...
              'Skylab','Skylab B','ISS'})
str = 

  2×3 string array

    "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 = 

  2×3 string array

    "Mercury     "    "Gemini      "    "Apollo      "
    "Skylab      "    "Skylab B    "    "ISS         "

Create a string array and pad the strings to the left.

str = string({'Mary';'Elizabeth';'James'})
str = 

  3×1 string array

    "Mary"
    "Elizabeth"
    "James"

newStr = pad(str,'left')
newStr = 

  3×1 string array

    "     Mary"
    "Elizabeth"
    "    James"

Pad both sides.

newStr = pad(str,'both')
newStr = 

  3×1 string array

    "  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 = 

  5×1 string array

    "69.45"
    "31.71"
    "95.36"
    "3.44"
    "7.82"

newStr = pad(str,7,'left','0')
newStr = 

  5×1 string array

    "0069.45"
    "0031.71"
    "0095.36"
    "0003.44"
    "0007.82"

Related Examples

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

Length to pad strings to, 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

More About

collapse all

Tall Array Support

This function supports tall arrays with the limitation:

If you do not specify width, then a full pass through the data is required to determine it.

For more information, see Tall Arrays.

See Also

| | | | |

Introduced in R2016b

Was this topic helpful?