eraseBetween — Delete text between boundary markers in MATLAB and RunMat.
eraseBetween(text, start, stop) removes text between two boundary markers. String/position markers, expansion rules, and 'Boundaries' option behavior follow MATLAB semantics.
Syntax
newText = eraseBetween(str, start, end)
newText = eraseBetween(str, start, end, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | Input text scalar/array/cell. |
start | Any | Yes | — | Start boundary marker text or positive integer position(s). |
end | Any | Yes | — | End boundary marker text or positive integer position(s). |
Name | StringScalar | Yes | — | Option name (`Boundaries`). |
Value | Any | Variadic | — | Option value and additional Name/Value pairs. |
Returns
| Name | Type | Description |
|---|---|---|
newText | Any | Text with between-boundary content erased, preserving text container semantics. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:eraseBetween:InvalidInput | First argument is not a string array, character array, or cell array of text scalars. | eraseBetween: first argument must be a string array, character array, or cell array of character vectors |
RunMat:eraseBetween:BoundaryType | Start/end boundaries are mixed text/numeric domains or use unsupported boundary types. | eraseBetween: start and end arguments must both be text or both be numeric positions |
RunMat:eraseBetween:PositionType | Numeric boundary positions are not positive finite integers. | eraseBetween: position arguments must be positive integers |
RunMat:eraseBetween:NameValuePair | Name/value options are not supplied in complete pairs. | eraseBetween: name-value arguments must appear in pairs |
RunMat:eraseBetween:OptionName | An option name other than `Boundaries` was supplied. | eraseBetween: unrecognized parameter name |
RunMat:eraseBetween:OptionValue | `Boundaries` option value is not `inclusive` or `exclusive`. | eraseBetween: 'Boundaries' must be either 'inclusive' or 'exclusive' |
RunMat:eraseBetween:CellElement | Cell text input/boundary contains non-text values or non-row char arrays. | eraseBetween: cell array elements must be string scalars or character vectors |
RunMat:eraseBetween:SizeMismatch | Text/boundary inputs are not broadcast-compatible for erase semantics. | eraseBetween: boundary sizes must be compatible with the text input |
RunMat:eraseBetween:InternalError | Internal output construction failed. | eraseBetween: internal error |
How eraseBetween works
- Accepts string scalars, string arrays, character arrays (row-by-row), and cell arrays containing string scalars or character vectors; the output keeps the same container type.
- Boundary arguments can be literal text, scalar pattern objects, or numeric positions. Both boundaries in a call must use the same representation—mixing text/pattern and numeric markers raises a size/type error. Broader pattern-array containers depend on shared pattern-array runtime representation and are not claimed by this implementation.
- Text boundaries are exclusive by default: the markers are preserved while the enclosed text is deleted. Numeric positions are inclusive by default: characters at
startPosandendPosare deleted together with the interior. 'Boundaries','inclusive'removes the markers themselves;'Boundaries','exclusive'keeps them. The option is case-insensitive and must be supplied as name-value pairs.- Missing string scalars propagate (the MATLAB
missingplaceholder in either boundary or text yields<missing>in the result). When a boundary cannot be located,eraseBetweenreturns the original element unchanged. - Numeric positions are validated as positive integers and interpreted using MATLAB’s 1-based indexing rules. All eight typed-integer classes are read exactly as documented numeric structural controls.
- MATLAB-compatible array forms require each boundary to be scalar or the same size as the text input. RunMat mode additionally permits general broadcast-compatible boundary shapes.
- Row-wise character matrices, cells containing string scalars, and GPU-resident position arrays are separately gated RunMat extensions.
Does RunMat run eraseBetween on the GPU?
The builtin performs text work on the CPU. In RunMat mode, resident numeric position controls gather through their authoritative storage and outputs remain on host; strict compatibility gates that extension before download.
GPU memory and residency
No text output remains resident. GPU-resident numeric position controls are a RunMat extension and are gathered exactly in RunMat mode; strict compatibility rejects them before provider access.
Examples
Removing text between substrings
txt = "The quick brown fox";
result = eraseBetween(txt, "quick", " fox")Expected output:
result = "The quick fox"Deleting substrings across a string array
str = ["The quick brown fox jumps"; "over the lazy dog"];
starts = ["quick"; "the"];
ends = [" fox"; " dog"];
trimmed = eraseBetween(str, starts, ends)Expected output:
trimmed = 2×1 string
"The quick fox jumps"
"over the dog"Removing characters between numeric positions
name = "Edgar Allen Poe";
short = eraseBetween(name, 6, 11)Expected output:
short = "Edgar Poe"Using inclusive boundaries to drop the markers
sentence = "The quick brown fox jumps over the lazy dog";
collapsed = eraseBetween(sentence, " brown", "lazy", "Boundaries", "inclusive")Expected output:
collapsed = "The quick dog"Operating on character arrays while preserving padding
chars = char("Server<GPU>", "Engine<CPU>");
trimmed = eraseBetween(chars, "<", ">", "Boundaries", "inclusive")Expected output:
trimmed =
2×6 char array
"Server"
"Engine"Preserving element types in cell arrays
C = {'alpha<1>', "beta<2>";
'gamma<3>', "delta<4>"};
clean = eraseBetween(C, "<", ">", "Boundaries", "inclusive")Expected output:
clean =
2×2 cell array
{'alpha'} {"beta"}
{'gamma'} {"delta"}Handling missing strings safely
texts = [missing, "Planner<GPU>"];
result = eraseBetween(texts, "<", ">")Expected output:
result = 1×2 string
"<missing>" "Planner"Using eraseBetween with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how eraseBetween changes the result.
Run a small eraseBetween example, explain the result, then change one input and compare the output.
FAQ
Which argument types does eraseBetween accept?⌄
The first argument can be a string scalar, string array, character array, or cell array of character vectors / string scalars. Boundary arguments must both be text markers or both be numeric positions.
What happens if a boundary is not found?⌄
The original text is returned unchanged. Missing string scalars also propagate unchanged.
How does 'Boundaries','inclusive' interact with text markers?⌄
Inclusive mode removes the matched start and end markers together with the enclosed text. Exclusive mode keeps the markers and removes only the interior.
Can I broadcast scalar boundaries across an array input?⌄
Scalar markers apply to every text element. Nonscalar boundary arrays must be the same size as the text input in MATLAB-compatible mode; RunMat mode separately enables general broadcast-compatible shapes.
Are GPU inputs supported?⌄
GPU values are gathered to host memory before processing. The builtin always returns host-resident outputs and is registered as an Accelerate sink, so fusion planning does not keep text on the GPU.
Does eraseBetween validate numeric positions?⌄
Yes. Positions are parsed exactly as positive integers using MATLAB’s 1-based indexing. The current public source does not fully specify every out-of-range case, so those details remain evidence-open rather than being presented as MATLAB guarantees.
Related Strings functions
Transform
append · deblank · erase · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · trainWordEmbedding · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp · strncmpi · strtok · unicode2native
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how eraseBetween is executed, line by line, in Rust.
- View the source for eraseBetween 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.