RunMat
GitHub

strrep — Replace non-overlapping substring occurrences in text inputs with MATLAB-compatible container behavior.

strrep(str, old, new) replaces every non-overlapping occurrence of old with new. It supports MATLAB-compatible string, char, and cellstr-style inputs with element-wise replacement semantics.

Syntax

newStr = strrep(str, old, new)

Inputs

NameTypeRequiredDefaultDescription
strAnyYesInput text (string/char/cell).
oldAnyYesPattern text scalar (string or char row).
newAnyYesReplacement text scalar matching old's data type family.

Returns

NameTypeDescription
newStrAnyText with pattern occurrences replaced, preserving input container kind.

Errors

IdentifierWhenMessage
RunMat:strrep:InvalidInputFirst argument is not a string array, char array, or cell array of text scalars.strrep: first argument must be a string array, character array, or cell array of character vectors
RunMat:strrep:PatternTypeold/new arguments are not string scalars or character vectors.strrep: old and new must be string scalars or character vectors
RunMat:strrep:PatternMismatchold and new are different text data families (string vs char).strrep: old and new must be the same data type

How strrep works

  • String scalars remain strings. Missing string values (<missing>) propagate unchanged.
  • String arrays are processed element-wise while preserving their full shape and orientation.
  • Character arrays are handled row by row. Rows expand or shrink as needed and are padded with spaces so the result stays a rectangular char array, just like MATLAB.
  • Cell arrays must contain character vectors or string scalars. The result is a cell array of identical size where each element has had the replacement applied.
  • The old and new arguments must be string scalars or character vectors of the same data type.
  • old can be empty. In that case, strrep inserts new before the first character, between existing characters, and after the final character.

Does RunMat run strrep on the GPU?

RunMat treats text replacement as a CPU-first workflow:

1. The builtin is registered as an Accelerate *sink*, so the planner gathers any GPU-resident inputs (string arrays, char arrays, or cell contents) back to host memory before work begins. 2. Replacements are computed entirely on the CPU, mirroring MATLAB’s behaviour and avoiding GPU/device divergence in string handling. 3. Results are returned as host values (string array, char array, or cell array). Residency is never pushed back to the GPU, keeping semantics deterministic regardless of the active provider.

GPU memory and residency

No. strrep registers as a sink with RunMat Accelerate, so the fusion planner never keeps its inputs or outputs on the GPU. Even if you start with GPU data, the runtime gathers it automatically—manual gpuArray or gather calls are unnecessary.

Examples

Replacing a word inside a string scalar

txt = "RunMat turbo mode";
result = strrep(txt, "turbo", "accelerate")

Expected output:

result = "RunMat accelerate mode"

Updating every element of a string array

labels = ["GPU planner", "CPU planner"];
updated = strrep(labels, "planner", "pipeline")

Expected output:

updated = 2×1 string
    "GPU pipeline"
    "CPU pipeline"

Preserving rectangular shape in character arrays

chars = char("alpha", "beta ");
out = strrep(chars, "a", "A")

Expected output:

out =

  2×5 char array

    'AlphA'
    'betA '

Applying replacements inside a cell array of character vectors

C = {'Kernel Fusion', 'GPU Planner'};
renamed = strrep(C, ' ', '_')

Expected output:

renamed = 1×2 cell array
    {'Kernel_Fusion'}    {'GPU_Planner'}

Inserting text with an empty search pattern

stub = "abc";
expanded = strrep(stub, "", "-")

Expected output:

expanded = "-a-b-c-"

Leaving missing string values untouched

vals = ["RunMat", "<missing>", "Accelerate"];
out = strrep(vals, "RunMat", "RUNMAT")

Expected output:

out = 1×3 string
    "RUNMAT"    <missing>    "Accelerate"

Replacing substrings gathered from GPU inputs

g = gpuArray("Turbine");
host = strrep(g, "bine", "bo")

Expected output:

host = "Turbo"

Using strrep with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how strrep changes the result.

Run a small strrep example, explain the result, then change one input and compare the output.

FAQ

Which input types does strrep accept?

String scalars, string arrays, character vectors, character arrays, and cell arrays of character vectors. The old and new arguments must be string scalars or character vectors of the same data type.

Does strrep support multiple search terms at once?

No. Use the newer replace builtin if you need to substitute several search terms in a single call.

How does strrep handle missing strings?

Missing string scalars remain <missing> and are returned unchanged, even when the search pattern matches ordinary text.

Will rows of a character array stay aligned?

Yes. Each row is replaced individually, then padded with spaces so that the overall array stays rectangular, matching MATLAB exactly.

What happens when old is empty?

RunMat mirrors MATLAB: new is inserted before the first character, between every existing character, and after the last character.

Does strrep run on the GPU?

Not today. The builtin gathers GPU-resident data to host memory automatically before performing the replacement logic.

Can I mix strings and character vectors for old and new?

No. MATLAB requires old and new to share the same data type. RunMat enforces the same rule and raises a descriptive error when they differ.

How do I replace text stored inside cell arrays?

strrep traverses the cell array, applying the replacement to each character vector or string scalar element and returning a cell array of the same shape.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strrep 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.