insertBefore — Insert text before each matching marker or a one-based position.
insertBefore(str,patOrPos,newText) inserts newText before every matching text or pattern marker, or before a one-based numeric position. A scalar marker/replacement applies to each element; nonscalar controls must match the text-element shape.
Syntax
newStr = insertBefore(str, patOrPos, newText)Inputs
str- Type
Any- Required
- Yes
- Description
- Input string array, character vector, or cell array of character vectors.
patOrPos- Type
Any- Required
- Yes
- Description
- Text or pattern marker, or positive one-based numeric position.
newText- Type
Any- Required
- Yes
- Description
- Text to insert; scalar or the same size as str.
Returns
out- Type
Any- Description
- Output value.
Errors
RunMat:insert:InvalidInput- When
- The call does not contain exactly one text input, one text or numeric boundary, and one replacement-text input.
- Message
text insertion: invalid input
RunMat:insert:InvalidBoundary- When
- A numeric position is not a positive integer or a textual boundary is invalid or absent.
- Message
text insertion: invalid boundary
RunMat:insert:SizeMismatch- When
- A nonscalar boundary or replacement-text array does not have the same shape as the input text array.
- Message
text insertion: array size must match input text
Text and boundary semantics
insertBefore(str,patOrPos,newText) inserts newText before every matching text or pattern marker, or before a one-based numeric position. A scalar marker/replacement applies to each element; nonscalar controls must match the text-element shape.
The current implementation supports scalar or same-size boundary/replacement arrays, first-class pattern objects as scalar markers, and host text. An absent marker or out-of-range numeric position raises a boundary error. Typed missing subject elements propagate unchanged; missing boundary or replacement controls are rejected. Literal <missing> text remains ordinary text.
MATLAB-compatible text inputs are string arrays, character vectors, and cell arrays of character vectors. RunMat mode additionally admits row-wise character matrices and cells containing string scalars or nested cells, subject to each identity's extension gate.
Text and pattern markers and real numeric positions are distinct boundary forms. All eight fixed-width integer classes are accepted as exact structural position controls; nonintegral, zero, and unsupported controls raise errors.
Placement and effects
Source and insertion values are host text. A GPU-resident position control can gather only after its RunMat extension is admitted; numeric or resident source/replacement text rejects before provider access.
This host text operation does not fuse or run a provider kernel. It may suspend when gathering an admitted position, can throw on malformed inputs or boundary errors, and does not use callbacks, workspace state, files, time, or randomness.
Examples
Insert at the beginning
value = insertBefore("run", 1, "pre")Expected output:
value = "prerun"Insert before every text match
part = insertBefore("bread cheese wine", " ", ",")Insert before a one-based position
part = insertBefore("James Maxwell", 7, "Clerk ")Insert elementwise in a string array
parts = insertBefore(["a.txt"; "b.csv"], ".", ["-x"; "-y"])Preserve a character vector
part = insertBefore('cat dog', ' dog', ',')Using insertBefore with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how insertBefore changes the result.
Run a small insertBefore example, explain the result, then change one input and compare the output.
FAQ
Does insertion affect every marker match?⌄
Yes. A text or pattern marker inserts newText at every match in each text element. A numeric position inserts once.
What if the marker is absent?⌄
The current RunMat implementation raises a boundary error.
Can replacement text vary by element?⌄
Yes. newText may be scalar or have the same shape as the source text elements.
Does insertion preserve input type?⌄
Yes for supported host string, character, and cell text containers.
Can numeric arrays be source or replacement text?⌄
No. Integer arrays are valid only as exact one-based position controls.
Does insertion run on GPU?⌄
No GPU text kernel exists. RunMat mode may gather an admitted resident numeric position; text remains on host.
Related Strings functions
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · 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
Source you can read
Every RunMat function's source is public. Read exactly how insertBefore is executed, line by line, in Rust.
- View the source for insertBefore in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.