strsplit — Split a string scalar or character vector into substrings using simple or regular-expression delimiters.
strsplit(str) splits a string scalar or character vector into substrings. RunMat implements the MATLAB-style scalar-text API, including 'CollapseDelimiters', 'DelimiterType', and the optional second output containing the matched delimiters.
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"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 works, line by line, in Rust.
- View strsplit.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.