strfind — Locate the starting indices of pattern matches in text inputs, returning doubles or cell arrays like MATLAB.
k = strfind(str, pattern) returns the starting indices of every occurrence of pattern inside str. The builtin mirrors MATLAB semantics for character vectors, string scalars, string arrays, and cell arrays of character vectors.
How strfind works in RunMat
- Accepts text inputs as string scalars/arrays, character vectors/arrays, or cell arrays of character vectors. Mixed combinations are permitted.
- Applies MATLAB-style implicit expansion when either argument is non-scalar.
- Returns a numeric row vector when both inputs are scalar text (character vectors or string scalars) and
'ForceCellOutput'isfalse. If either input is a cell array or the broadcasted result contains multiple elements, the output is a cell array with the broadcasted shape, and each cell contains a row vector of double indices. - Pattern matches are case-sensitive and may overlap. For example,
strfind("aaaa","aa")yields[1 2 3]. - An empty pattern matches the boundaries between characters, producing indices
1:length(str)+1. Missing strings (<missing>) never match any pattern. - Specify
'ForceCellOutput', trueto always obtain a cell array, even for scalar inputs.
How strfind runs on the GPU
strfind performs substring searches on the host CPU. When its inputs currently live on the GPU (for example, after other accelerated operations), RunMat gathers them automatically before executing the search so the behaviour matches MATLAB exactly. No provider hooks are required.
GPU memory and residency
You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).
strfind always executes on the host, but if you pass in GPU-resident values RunMat gathers them automatically before performing the search. This keeps the results identical to MATLAB while still allowing upstream computations to benefit from acceleration.
Examples
Find substring positions in a character vector
idx = strfind('abracadabra', 'abra')Expected output:
idx = [1 8]Locate overlapping matches
idx = strfind("aaaa", "aa")Expected output:
idx = [1 2 3]Return matches for each element of a string array
words = ["hydrogen"; "helium"; "lithium"];
idx = strfind(words, "i")Expected output:
idx = 3×1 cell array
{[]}
{[4]}
{[2 5]}Search with multiple patterns against one subject
idx = strfind("saturn", ["sat", "turn", "moon"])Expected output:
idx = 1×3 cell array
{[1]} {[3]} {[]}Force cell output for scalar inputs
idx = strfind('mission', 's', 'ForceCellOutput', true)Expected output:
idx = 1×1 cell array
{[3 4]}Handle empty patterns and missing strings
idxEmpty = strfind("abc", "");
idxMissing = strfind("<missing>", "abc")Expected output:
idxEmpty = [1 2 3 4];
idxMissing = []FAQ
What types can I pass to strfind?
Use string scalars/arrays, character vectors/arrays, or cell arrays of character vectors for either argument. Mixed combinations are allowed; MATLAB-style implicit expansion aligns the inputs automatically.
How are the results formatted?
When both inputs are scalar text (character vectors or string scalars) and 'ForceCellOutput' is false, strfind returns a numeric row vector with the starting indices. In all other cases — for example, if either input is a cell array or the broadcasted size exceeds one element — the result is a cell array whose shape matches the broadcasted size, with each cell containing a row vector of doubles.
Do matches overlap?
Yes. The builtin considers every occurrence of the pattern, including overlapping matches.
What happens with empty patterns?
An empty pattern matches the gaps between characters, so the indices 1:length(str)+1 are returned for non-missing text. When the subject is missing, the result is an empty array.
How do I always get a cell array?
Supply 'ForceCellOutput', true. This mirrors MATLAB and is useful when you want consistent cell outputs regardless of input sizes.
Does strfind support case-insensitive matching?
No. strfind is case-sensitive like MATLAB. Use contains, startsWith, or the regular expression functions when you need case-insensitive searches.
Related functions to explore
These functions work well alongside strfind. Each page has runnable examples you can try in the browser.
contains, startsWith, endsWith, regexp
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strfind works, line by line, in Rust.
- View strfind.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.