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

Pad String Array with Spaces

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 = 2x3 string "Mercury " "Gemini " "Apollo " "Skylab " "Skylab B" "ISS "

Pad String Array to Specified Length

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 = 2x3 string "Mercury " "Gemini " "Apollo " "Skylab " "Skylab B " "ISS "

Pad Strings to Different Sides

Create a string array.

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

str = 3x1 string "Mary" "Elizabeth" "James"

Pad the strings to the left.

newStr = 3x1 string " Mary" "Elizabeth" " James"

Pad both sides.

newStr = 3x1 string " Mary " "Elizabeth" " James "

Pad String Array with Different Character

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

str — Input text

string array | character vector | cell array of character vectors

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

Data Types: string | char | cell

numberOfCharacters — Total number of characters in output strings

positive integer

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 — Side of string to pad

'right' (default) | 'left' | 'both'

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

padCharacter — Character to pad with

' ' (default) | character | 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

newStr — Output text

string array | character vector | cell array of character vectors

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

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

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

For more information, see Tall Arrays.

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2016b