Main Content

strip

Remove leading and trailing characters from strings

Description

example

newStr = strip(str) removes all consecutive whitespace characters from the beginning and end of str, and returns the result as newStr.

example

newStr = strip(str,side) removes all consecutive whitespace characters from the side specified by side. The side argument can be 'left', 'right', or 'both'.

example

newStr = strip(___,stripCharacter) strips the character specified by stripCharacter, instead of whitespace characters. You can use any of the input arguments in the previous syntaxes.

Examples

collapse all

Create a string array.

str = ["Ann Marie      ";
       "          James";
       "Pauline        "]
str = 3x1 string
    "Ann Marie      "
    "          James"
    "Pauline        "

Delete the leading and trailing space characters in each string.

newStr = strip(str)
newStr = 3x1 string
    "Ann Marie"
    "James"
    "Pauline"

Create a string array.

str = ["   Ann Marie   ";
       "   James       ";
       "   Pauline     "]
str = 3x1 string
    "   Ann Marie   "
    "   James       "
    "   Pauline     "

Delete space characters from the right side only.

newStr = strip(str,'right')
newStr = 3x1 string
    "   Ann Marie"
    "   James"
    "   Pauline"

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

str = ["0095.36";
       "0003.44";
       "0007.82"]
str = 3x1 string
    "0095.36"
    "0003.44"
    "0007.82"

Delete the leading zeroes.

newStr = strip(str,'left','0')
newStr = 3x1 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 and newStr 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

Version History

Introduced in R2016b