strsplit — Split scalar text into substrings using simple or regular-expression delimiters with MATLAB-compatible options.
strsplit(str) splits string-scalar or character-vector text into substrings. It supports MATLAB-compatible delimiter modes, delimiter collapsing, and optional matched-delimiter outputs.
Syntax
[parts, matches] = strsplit(str)
[parts, matches] = strsplit(str, delimiter)
[parts, matches] = strsplit(str, delimiter, Name, Value, ...)
[parts, matches] = strsplit(str, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | String scalar or character vector input. |
delimiter | Any | Yes | — | Delimiter scalar/array/cell. |
Name | StringScalar | Yes | — | Option name (`CollapseDelimiters` or `DelimiterType`). |
Value | Any | Variadic | — | Option values and additional Name/Value pairs. |
Returns
| Name | Type | Description |
|---|---|---|
parts | Any | Split tokens. |
matches | Any | Matched delimiters when requested as second output. |
Returned values from strsplit depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:strsplit:InvalidInput | First argument is not a string scalar or character vector. | strsplit: first argument must be a string scalar or character vector |
RunMat:strsplit:DelimiterType | Delimiter input is not a supported text scalar/array/cell. | strsplit: delimiter must be a character vector, string scalar, string array, or cell array of character vectors |
RunMat:strsplit:NameValuePair | Name-value options are not supplied in complete pairs. | strsplit: name-value arguments must be supplied in pairs |
RunMat:strsplit:UnknownName | An option name is not recognized. | strsplit: unrecognized name-value argument; supported names are 'CollapseDelimiters' and 'DelimiterType' |
RunMat:strsplit:EmptyDelimiter | Delimiter list is empty or contains empty delimiter entries. | strsplit: delimiters must contain at least one character |
RunMat:strsplit:DelimiterMode | DelimiterType option is not `Simple` or `RegularExpression`. | strsplit: value for 'DelimiterType' must be 'Simple' or 'RegularExpression' |
RunMat:strsplit:OptionValue | Option values are not logical true/false values. | strsplit: option values must be logical true or false |
RunMat:strsplit:RegexInvalid | Regular expression delimiter pattern fails to compile. | strsplit: invalid delimiter regular expression |
RunMat:strsplit:InternalError | Internal output container construction failed. | strsplit: internal error |
How strsplit works
- The first argument must be scalar text: a string scalar or a character vector. Unlike
split,strsplitdoes not accept string arrays, character matrices, or cell arrays of text. - When you omit the delimiter,
strsplitsplits on MATLAB's default whitespace set (' ',\f,\n,\r,\t,\v) and collapses consecutive delimiters. - Explicit delimiters default to
'CollapseDelimiters', true, so adjacent delimiters are grouped unless you opt out. - Set
'DelimiterType', 'RegularExpression'to interpret delimiters as regex patterns. In'Simple'mode, delimiters are matched literally and special regex characters are escaped. - String input returns a row string array. Character-vector input returns a row cell array of text elements.
Examples
Split A Character Vector On Whitespace
txt = 'The rain in Spain.';
parts = strsplit(txt)Expected output:
parts = 1x4 cell
{'The'} {'rain'} {'in'} {'Spain.'}Split Using A Literal Delimiter
csv = "alpha,beta,gamma";
parts = strsplit(csv, ",")Expected output:
parts = 1x3 string
"alpha" "beta" "gamma"Return The Matched Delimiters
[parts, matches] = strsplit("a,,b,", ",")Expected output:
parts = 1x3 string
"a" "b" ""
matches = 1x2 string
",," ","Use A Regular Expression Delimiter
[parts, matches] = strsplit("1.21m/s 1.985 m/s", "\s*m/s\s*", "DelimiterType", "RegularExpression")Expected output:
parts = 1x3 string
"1.21" "1.985" ""
matches = 1x2 string
"m/s " " m/s"Preserve Empty Fields
parts = strsplit("a,,b", ",", "CollapseDelimiters", false)Expected output:
parts = 1x3 string
"a" "" "b"Using strsplit with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how strsplit changes the result.
Run a small strsplit example, explain the result, then change one input and compare the output.
FAQ
How is strsplit different from split?⌄
strsplit is the scalar-text API that mirrors MATLAB's older function shape, including optional matches output and regex delimiter mode. split is the vectorized string-array builtin.
What does the second output contain?⌄
The optional matches output returns the delimiter text that produced each split. When delimiters are collapsed, adjacent delimiter runs are grouped into the matched text.
Can strsplit use regular expressions?⌄
Yes. Pass 'DelimiterType', 'RegularExpression' and provide delimiter patterns as regex fragments.
Does strsplit accept string arrays?⌄
No. RunMat currently restricts strsplit to scalar text, matching MATLAB's documented scalar-input API.
Related Strings functions
Transform
erase · eraseBetween · extractBetween · join · lower · pad · replace · split · strcat · strip · strjoin · strrep · strtrim · upper
Core
char · compose · num2str · sprintf · str2double · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp
Search
contains · endsWith · startsWith · strfind
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strsplit is executed, line by line, in Rust.
- View the source for strsplit in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.