添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • Syntax
  • Description
  • Examples
  • Input Arguments
  • Algorithms
  • Alternative Functionality
  • Extended Capabilities
  • Version History
  • See Also
  • strtrim

    Remove leading and trailing whitespace from strings

    collapse all in page

    Description

    example

    Note

    strip is recommended over strtrim because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality .

    newStr = strtrim( str ) removes leading and trailing whitespace characters from str and returns the result as newStr . However, strtrim does not remove significant whitespace characters. For example, strtrim removes leading and trailing space and tab characters, but does not remove the nonbreaking space character, char(160) .

    Examples

    collapse all

    Create a character vector with spaces and a tab character as leading whitespace.

    chr = sprintf('  \t   Remove    leading whitespace')
    chr = 
    '  	   Remove    leading whitespace'
    

    Remove the leading tab and spaces.

    newChr = strtrim(chr)
    newChr = 
    'Remove    leading whitespace'
    

    strtrim removes the leading whitespace characters, but not the whitespace between other characters.

    Create a string array.

    str = ["   Gemini    ","   Apollo    ";
           "   ISS       ","   Skylab    "]
    str = 2x2 string
        "   Gemini    "    "   Apollo    "
        "   ISS       "    "   Skylab    "
    

    Remove leading and trailing whitespace with the strtrim function.

    newStr = strtrim(str)
    newStr = 2x2 string
        "Gemini"    "Apollo"
        "ISS"       "Skylab"
    

    Remove the leading and trailing whitespace from all the character vectors in a cell array and display them.

    chr = {'     Trim leading whitespace';
           'Trim trailing whitespace     '}
    chr = 2x1 cell
        {'     Trim leading whitespace' }
        {'Trim trailing whitespace     '}
    
    newChr = strtrim(chr)
    newChr = 2x1 cell
        {'Trim leading whitespace' }
        {'Trim trailing whitespace'}
    

    Create a character vector that includes the nonbreaking space character, char(160) , as a trailing whitespace character.

    chr = '     Keep nonbreaking space';
    chr = [chr char(160) '     '];

    Display chr between | symbols to show the leading and trailing whitespace.

    ['|' chr '|']
    ans = 
    '|     Keep nonbreaking space      |'
    

    Remove the leading and trailing whitespace characters.

    newChr = strtrim(chr);

    Display newChr between | symbols. strtrim removes the space characters but leaves the nonbreaking space at the end of newChr .

    ['|' newChr '|']
    ans = 
    '|Keep nonbreaking space |'
    

    Input Arguments

    collapse all

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

    Algorithms

    strtrim 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

    Alternative Functionality

    Update code that makes use of strtrim to use strip instead. For example:

    Not Recommended Recommended
    str = "     test  ";
    newStr = strtrim(str)
    newStr = 
        "test"
    str = "     test  ";
    newStr = strip(str)
    newStr = 
        "test"

    Extended Capabilities

    Version History

    Introduced before R2006a

    You clicked a link that corresponds to this MATLAB command:

    Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.