Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

strsplit

Split string at specified delimiter

Syntax

  • C = strsplit(str)
    example
  • C = strsplit(str,delimiter)
    example
  • C = strsplit(str,delimiter,Name,Value)
    example
  • [C,matches] = strsplit(___)
    example

Description

example

C = strsplit(str) splits str at whitespace into C. A whitespace character is equivalent to any sequence in the set {' ','\f','\n','\r','\t','\v'}.

example

C = strsplit(str,delimiter) splits str at the delimiters specified by delimiter.

example

C = strsplit(str,delimiter,Name,Value) specifies additional delimiter options using one or more name-value pair arguments.

example

[C,matches] = strsplit(___) additionally returns the array,matches. The matches output argument contains all occurrences of delimiters upon which strsplit splits str. You can use this syntax with any of the input arguments of the previous syntaxes.

Examples

collapse all

str = 'The rain in Spain.';
C = strsplit(str)
C =

  1×4 cell array

    'The'    'rain'    'in'    'Spain.'

C is a cell array containing four character vectors.

Split a character vector that contains comma-separated values.

data = '1.21, 1.985, 1.955, 2.015, 1.885';
C = strsplit(data,', ')
C =

  1×5 cell array

    '1.21'    '1.985'    '1.955'    '2.015'    '1.885'

Split a character vector, data, which contains the units m/s with an arbitrary number of whitespace on either side of the text. The regular expression, \s*, matches any whitespace character appearing zero or more times.

data = '1.21m/s1.985m/s 1.955 m/s2.015 m/s 1.885m/s';
[C,matches] = strsplit(data,'\s*m/s\s*',...
    'DelimiterType','RegularExpression')
C =

  1×6 cell array

    '1.21'    '1.985'    '1.955'    '2.015'    '1.885'    ''


matches =

  1×5 cell array

    'm/s'    'm/s '    ' m/s'    ' m/s '    'm/s'

In this case, the last character vector in C is empty. This empty character vector follows the last matched delimiter.

myPath = 'C:\work\matlab';
C = strsplit(myPath,'\')
C =

  1×3 cell array

    'C:'    'work'    'matlab'

Split a character vector on ' ' and 'ain', treating multiple delimiters as one. Specify multiple delimiters in a cell array of character vectors.

str = 'The rain in Spain stays mainly in the plain.';
[C,matches] = strsplit(str,{' ','ain'},'CollapseDelimiters',true)
C =

  1×11 cell array

  Columns 1 through 9

    'The'    'r'    'in'    'Sp'    'stays'    'm'    'ly'    'in'    'the'

  Columns 10 through 11

    'pl'    '.'


matches =

  1×10 cell array

  Columns 1 through 9

    ' '    'ain '    ' '    'ain '    ' '    'ain'    ' '    ' '    ' '

  Column 10

    'ain'

Split the same character vector on whitespace and on 'ain', using regular expressions and treating multiple delimiters separately.

[C,matches] = strsplit(str,{'\s','ain'},'CollapseDelimiters',...
    false, 'DelimiterType','RegularExpression')
C =

  1×13 cell array

  Columns 1 through 10

    'The'    'r'    ''    'in'    'Sp'    ''    'stays'    'm'    'ly'    'in'

  Columns 11 through 13

    'the'    'pl'    '.'


matches =

  1×12 cell array

  Columns 1 through 10

    ' '    'ain'    ' '    ' '    'ain'    ' '    ' '    'ain'    ' '    ' '

  Columns 11 through 12

    ' '    'ain'

In this case, strsplit treats the two delimiters separately, so empty character vectors appear in output C between the consecutively matched delimiters.

Split text on the character vectors ', ' and ', and '.

str = 'bacon, lettuce, and tomato';
[C,matches] = strsplit(str,{', ',', and '})
C =

  1×3 cell array

    'bacon'    'lettuce'    'and tomato'


matches =

  1×2 cell array

    ', '    ', '

Because the command lists ', ' first and ', and ' contains ', ', the strsplit function splits str on the first delimiter and never proceeds to the second delimiter.

If you reverse the order of delimiters, ', and ' takes priority.

str = 'bacon, lettuce, and tomato';
[C,matches] = strsplit(str,{', and ',', '})
C =

  1×3 cell array

    'bacon'    'lettuce'    'tomato'


matches =

  1×2 cell array

    ', '    ', and '

Input Arguments

collapse all

Input text, specified as a character vector or a string scalar.

Data Types: char | string

Delimiting characters, specified as a character vector, a 1-by-N cell array of character vectors, or a 1-by-N string array. Text specified in delimiter does not appear in the output C.

Specify multiple delimiters in a cell array or a string array. The strsplit function splits str on the elements of delimiter. The order in which delimiters appear in delimiter does not matter unless multiple delimiters begin a match at the same character in str. In that case strsplit splits on the first matching delimiter in delimiter.

delimiter can include the following escape sequences:

\\

Backslash

\0

Null

\a

Alarm

\b

Backspace

\f

Form feed

\n

New line

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

Example: ','

Example: {'-',','}

Data Types: char | cell | string

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'DelimiterType','RegularExpression' instructs strsplit to treat delimiter as a regular expression.

collapse all

Multiple delimiter handling, specified as the comma-separated pair consisting of 'CollapseDelimiters' and either true or false. If true, then consecutive delimiters in str are treated as one. If false, then consecutive delimiters are treated as separate delimiters, resulting in empty character vector '' elements between matched delimiters.

Example: 'CollapseDelimiters',true

Delimiter type, specified as the comma-separated pair consisting of 'DelimiterType' and one of the following character vectors.

'Simple'Except for escape sequences, strsplit treats delimiter as literal text..
'RegularExpression'strsplit treats delimiter as a regular expression.

In both cases, delimiter can include escape sequences.

Output Arguments

collapse all

Parts of the original character vector, returned as a cell array of character vectors or as a string array. C always contains one more element than matches contains. Therefore, if str begins with a delimiter, then the first element of C contains no characters. If str ends with a delimiter, then the last cell in C contains no characters.

Identified delimiters, returned as a cell array of character vectors or as a string array. matches always contains one fewer element than output C contains. If str is a character vector or a cell array of character vectors, then matches is a cell array. If str is a string array, then matches is a string array.

More About

collapse all

Tips

  • Starting in R2016b, the split function is recommended to split elements of a string array.

See Also

| | |

Introduced in R2013a

Was this topic helpful?