RunMat
GitHub

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

NameTypeRequiredDefaultDescription
strAnyYesString scalar or character vector input.
delimiterAnyYesDelimiter scalar/array/cell.
NameStringScalarYesOption name (`CollapseDelimiters` or `DelimiterType`).
ValueAnyVariadicOption values and additional Name/Value pairs.

Returns

NameTypeDescription
partsAnySplit tokens.
matchesAnyMatched delimiters when requested as second output.

Returned values from strsplit depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:strsplit:InvalidInputFirst argument is not a string scalar or character vector.strsplit: first argument must be a string scalar or character vector
RunMat:strsplit:DelimiterTypeDelimiter 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:NameValuePairName-value options are not supplied in complete pairs.strsplit: name-value arguments must be supplied in pairs

How strsplit works

  • The first argument must be scalar text: a string scalar or a character vector. Unlike split, strsplit does not accept string arrays, character matrices, or cell arrays of text.
  • When you omit the delimiter, strsplit splits 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.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strsplit is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.