Method Reference: string.pad

string: newstr = pad (str)
string: newstr = pad (str, nchars)
string: newstr = pad (str, side)
string: newstr = pad (str, nchars, side)
string: newstr = pad (…, padchar)

Add leading or trailing characters to string array.

newstr = pad (str) pads each element of str with trailing spaces so that every element is as long as the longest one.

newstr = pad (str, nchars) pads each element to nchars characters. Elements that already have more than nchars characters are left unchanged; pad never truncates.

newstr = pad (str, side) and newstr = pad (str, nchars, side) add the padding on the side given by side, which can be "left", "right", or "both". The default is "right"; for "both", an odd number of pad characters places the extra one on the right.

newstr = pad (…, padchar) pads with the single character padchar instead of a space. As padchar is the last argument and a single character, it is told apart from side by its length.

Length is measured in characters, not bytes. newstr is a string array of the same size as str, and missing values in str are preserved.

Source Code: string

Example: 1

pad pads each string with spaces to a common width — handy for aligning text into columns.

 s = string ({'a'; 'bb'; 'ccc'})
s =
  3x1 string array

    "a"      
    "bb"     
    "ccc"
 pad (s, 5)
ans =
  3x1 string array

    "a    "    
    "bb   "    
    "ccc  "