Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

pad

Add leading or trailing characters to strings

Syntax

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

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

Create a string array. Starting in R2017a, you can create strings using double quotes instead of the string function.

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

Pad the elements of str with space characters.

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

Create a string array. Starting in R2017a, you can create strings using double quotes instead of the string function.

str = ["Mercury","Gemini","Apollo";
       "Skylab","Skylab B","ISS"]
str = 2x3 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 = 2x3 string array
    "Mercury     "    "Gemini      "    "Apollo      "
    "Skylab      "    "Skylab B    "    "ISS         "

Create a string array. Starting in R2017a, you can create strings using double quotes instead of the string function.

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

Pad the strings to the left.

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

Pad both sides.

newStr = pad(str,'both')
newStr = 3x1 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 = 5x1 string array
    "69.45"
    "31.71"
    "95.36"
    "3.44"
    "7.82"

newStr = pad(str,7,'left','0')
newStr = 5x1 string array
    "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

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

Extended Capabilities

Introduced in R2016b

Was this topic helpful?