strip - Remove leading and trailing characters from strings - MATLAB (original) (raw)

Remove leading and trailing characters from strings

Syntax

Description

[newStr](#bu%5F4ws9-newStr) = strip([str](#bu%5F4ws9-str)) removes all consecutive whitespace characters from the beginning and end ofstr, and returns the result asnewStr.

example

[newStr](#bu%5F4ws9-newStr) = strip([str](#bu%5F4ws9-str),[side](#bu%5F4ws9-side)) removes all consecutive whitespace characters from the side specified byside. The side argument can be'left', 'right', or'both'.

example

[newStr](#bu%5F4ws9-newStr) = strip(___,[stripCharacter](#bu%5F4ws9-stripCharacter)) strips the character specified by stripCharacter, instead of whitespace characters. You can use any of the input arguments in the previous syntaxes.

example

Examples

collapse all

Create a string array.

str = ["Ann Marie "; " James"; "Pauline "]

str = 3×1 string "Ann Marie " " James" "Pauline "

Delete the leading and trailing space characters in each string.

newStr = 3×1 string "Ann Marie" "James" "Pauline"

Create a string array.

str = [" Ann Marie "; " James "; " Pauline "]

str = 3×1 string " Ann Marie " " James " " Pauline "

Delete space characters from the right side only.

newStr = strip(str,'right')

newStr = 3×1 string " Ann Marie" " James" " Pauline"

Create a string array with elements that represent numbers. The strings include leading zeros that make them all the same length.

str = ["0095.36"; "0003.44"; "0007.82"]

str = 3×1 string "0095.36" "0003.44" "0007.82"

Delete the leading zeros.

newStr = strip(str,'left','0')

newStr = 3×1 string "95.36" "3.44" "7.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

Side of string to strip, specified as 'left','right', or 'both'. The default behavior of strip is to strip characters from both the left and the right side of the input text.

Data Types: char | string

Character to strip from input text, 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

Algorithms

strip does not remove significant whitespace characters.

This table shows the most common characters that are significant whitespace characters and their descriptions. For more information, see Whitespace character.

Significant Whitespace Character Description
char(133) Next line
char(160) Nonbreaking space
char(8199) Figure space
char(8239) Narrow no-break space

Extended Capabilities

expand all

Thestrip function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

Version History

Introduced in R2016b