pad - Add leading or trailing characters to strings - MATLAB (original) (raw)

Add leading or trailing characters to strings

Syntax

Description

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

example

[newStr](#bu76ql6-newStr) = pad([str](#bu76ql6-str),[numberOfCharacters](#bu76ql6-length)) adds space characters so the strings in newStr have the length specified by numberOfCharacters. If any strings instr have more characters thannumberOfCharacters, then pad does not modify them.

example

[newStr](#bu76ql6-newStr) = pad([str](#bu76ql6-str),[side](#bu76ql6-side)) adds space characters to the side specified by side. Theside argument can be 'left','right', or 'both'.

example

[newStr](#bu76ql6-newStr) = pad([str](#bu76ql6-str),[numberOfCharacters](#bu76ql6-length),[side](#bu76ql6-side)) adds space characters to the side specified by side, up to the length specified by numberOfCharacters.

[newStr](#bu76ql6-newStr) = pad(___,[padCharacter](#bu76ql6-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, thenpad(str,padCharacter) returns str unaltered.

example

Examples

collapse all

Create a string array.

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

str = 2×3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"

Pad the elements of str with space characters.

newStr = 2×3 string "Mercury " "Gemini " "Apollo " "Skylab " "Skylab B" "ISS "

Create a string array.

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

str = 2×3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"

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

newStr = 2×3 string "Mercury " "Gemini " "Apollo " "Skylab " "Skylab B " "ISS "

Create a string array.

str = ["Mary";"Elizabeth";"James"]

str = 3×1 string "Mary" "Elizabeth" "James"

Pad the strings to the left.

newStr = 3×1 string " Mary" "Elizabeth" " James"

Pad both sides.

newStr = 3×1 string " Mary " "Elizabeth" " James "

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

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

str = 5×1 string "69.45" "31.71" "95.36" "3.44" "7.82"

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

newStr = 5×1 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 andnewStr are the same data type.

Data Types: string | char | cell

Extended Capabilities

expand all

Thepad function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

Version History

Introduced in R2016b