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.

strip

Remove leading and trailing characters from string

Syntax

newStr = strip(str)
newStr = strip(str,side)
newStr = strip(___,stripCharacter)

Description

example

newStr = strip(str) removes all consecutive whitespace characters from the beginning and end of each string in 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 the space character. You can use any of the input arguments in the previous syntaxes.

Examples

collapse all

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

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

Delete the leading and trailing space characters in each string.

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

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

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

Delete space characters from the right side only.

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

Create a string array with elements that represent numbers. Starting in R2017a, you can create strings using double quotes instead of the string function. The strings include leading zeroes that make them all the same length.

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

Delete the leading zeroes.

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

Extended Capabilities

Introduced in R2016b

Was this topic helpful?