Documentation Center

  • Trial Software
  • Product Updates

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 the string, str, at whitespace into the cell array of strings, 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 a cell array of strings, matches, using any of the input arguments in the previous syntaxes. matches contains all occurrences of delimiters upon which strsplit splits str.

Examples

expand all

Split String on Whitespace

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

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

C is a cell array containing 4 strings.

Split String of Values on Specific Delimiter

Split a string of comma-separated values.

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

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

Split a string of values, data, which contains the units m/s with an arbitrary number of white-space on either side of the text. The regular expression, \s*, matches any white-space 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.21'    '1.985'    '1.955'    '2.015'    '1.885'    ''


matches = 

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

In this case, the last string in C is empty. This empty string is the string that follows the last matched delimiter.

Split Path String on File Separator

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

    'C:'    'work'    'matlab'

Split Text String with Multiple Delimiters

Split a string on ' ' and 'ain', treating multiple delimiters as one. Specify multiple delimiters in a cell array of strings.

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

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


matches = 

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

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

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

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


matches = 

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

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

Split Text with Multiple, Overlapping Delimiters

Split text on the strings ', ' and ', and '.

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

    'bacon'    'lettuce'    'and tomato'


matches = 

    ', '    ', '

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 = 

    'bacon'    'lettuce'    'tomato'


matches = 

    ', '    ', and '

Input Arguments

expand all

str — Input textstring

Input text, specified as a string.

Data Types: char

delimiter — Delimiting charactersstring | 1-by-n cell array of strings

Delimiting characters, specified as a single string or a 1-by-n cell array of strings. Strings specified in delimiter do not appear in the string fragments of the output C.

Specify multiple delimiters in a cell array of strings. Each element of the cell array must contain a single string in a single row. The strsplit function splits str on the elements of delimiter in the order in which they appear in the cell array.

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

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.

'CollapseDelimiters' — Multiple delimiter handling1 (true) (default) | 0 (false)

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 string '' elements between matched delimiters.

Example: 'CollapseDelimiters',true

'DelimiterType' — Delimiter type'Simple' (default) | 'RegularExpression'

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

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

In both cases, delimiter can include escape sequences.

Output Arguments

expand all

C — Parts of original stringcell array of strings

Parts of the original string, returned as a cell array of strings. C always contains one more element than matches contains. Consequently, if the original string, str, ends with a delimiter, then the last cell in C contains an empty string.

matches — Identified delimiters cell array of strings

Identified delimiters, returned as a cell array of strings. matches always contains one fewer element than output C contains.

See Also

| |

Was this topic helpful?