strip — Remove leading and trailing characters from text inputs with MATLAB-compatible side and character-set options.
strip(text) removes leading/trailing whitespace by default across string, char, and cellstr-style inputs. Optional arguments control side selection ('left', 'right', 'both') and custom character sets, matching MATLAB semantics.
Syntax
out = strip(str)
out = strip(str, direction)
out = strip(str, stripCharacters)
out = strip(str, direction, stripCharacters)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
str | Any | Yes | — | String/char/cell text input to strip. |
direction | StringScalar | Yes | "both" | Direction (`"left"|"right"|"both"`, plus leading/trailing synonyms). |
stripCharacters | Any | Yes | — | Characters to strip (scalar or per-element text container). |
direction | StringScalar | Yes | — | Direction (`"left"|"right"|"both"`, plus leading/trailing synonyms). |
Returns
| Name | Type | Description |
|---|---|---|
out | Any | Stripped text preserving input container kind and shape. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:strip:InvalidInput | Input is not a string array, character array, or cell array of text scalars. | strip: first argument must be a string array, character array, or cell array of character vectors |
RunMat:strip:CellElement | Cell array contains a non-text element or non-row char array element. | strip: cell array elements must be string scalars or character vectors |
RunMat:strip:InvalidDirection | Direction argument is not one of left/right/both (or leading/trailing synonyms). | strip: direction must be 'left', 'right', or 'both' |
RunMat:strip:InvalidCharacters | stripCharacters argument is not a valid text container. | strip: characters to remove must be a string array, character vector, or cell array of character vectors |
RunMat:strip:SizeMismatch | Per-element stripCharacters does not match input shape/size. | strip: stripCharacters must be the same size as the input when supplying multiple values |
RunMat:strip:ArgCount | More than three input arguments were supplied. | strip: too many input arguments |
RunMat:strip:InternalError | Internal output container construction failed. | strip: internal error |
How strip works
- By default,
stripremoves leading and trailing whitespace determined byisspace. - Direction keywords are case-insensitive.
'left'/'leading'trim the beginning,'right'/'trailing'trim the end, and'both'removes characters on both sides. - Provide a second argument containing characters to remove to strip those characters instead of whitespace. Supply a scalar string/char vector to apply the same rule to every element or a string / cell array matching the input size to specify element-wise character sets.
- Missing string scalars remain
<missing>. - Character arrays shrink or retain their width to match the longest stripped row; shorter rows are padded with spaces so the output stays rectangular.
- Cell arrays must contain string scalars or character vectors. Results preserve the original cell layout with trimmed elements.
Does RunMat run strip on the GPU?
strip executes on the CPU. When the input or any nested element resides on the GPU, RunMat gathers those values to host memory before trimming so the results match MATLAB exactly. Providers do not need to implement device kernels for this builtin today.
GPU memory and residency
Text data typically lives on the host. If you deliberately store text on the GPU (for example, by keeping character code points in device buffers), RunMat gathers them automatically when strip runs. You do not need to call gpuArray or gather manually for this builtin.
Examples
Remove Leading And Trailing Spaces From A String Scalar
name = " RunMat ";
clean = strip(name)Expected output:
clean = "RunMat"Trim Only The Right Side Of Each String
labels = [" Alpha "; " Beta "; " Gamma "];
right_stripped = strip(labels, 'right')Expected output:
right_stripped = 3×1 string
" Alpha"
" Beta"
" Gamma"Remove Leading Zeros While Preserving Trailing Digits
codes = ["00095"; "00137"; "00420"];
numeric = strip(codes, 'left', '0')Expected output:
numeric = 3×1 string
"95"
"137"
"420"Strip Character Arrays And Preserve Rectangular Shape
animals = char(" cat ", " dog", "cow ");
trimmed = strip(animals)Expected output:
trimmed =
3×4 char array
'cat '
'dog '
'cow 'Supply Per-Element Characters To Remove
metrics = ["##pass##", "--warn--", "**fail**"];
per_char = ["#"; "-"; "*"];
normalized = strip(metrics, 'both', per_char)Expected output:
normalized = 3×1 string
"pass"
"warn"
"fail"Trim Cell Array Elements With Mixed Types
pieces = {' GPU ', " Accelerate", 'RunMat '};
out = strip(pieces)Expected output:
out = 1×3 cell array
{'GPU'} {"Accelerate"} {'RunMat'}Using strip with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how strip changes the result.
Run a small strip example, explain the result, then change one input and compare the output.
FAQ
Which direction keywords are supported?⌄
'left' and 'leading' trim the beginning of the text, 'right' and 'trailing' trim the end, and 'both' (the default) trims both sides.
How do I remove characters other than whitespace?⌄
Provide a second argument containing the characters to remove, for example strip(str, "xyz") removes any leading or trailing x, y, or z characters. Combine it with a direction argument to control which side is affected.
Can I specify different characters for each element?⌄
Yes. Pass a string array or cell array of character vectors that matches the size of the input. Each element is trimmed using the corresponding character set.
What happens to missing strings?⌄
Missing string scalars (string(missing)) remain <missing> exactly as in MATLAB.
Does strip change the shape of character arrays?⌄
Only the width can change. strip keeps the same number of rows and pads shorter rows with spaces so the array stays rectangular.
Will strip run on the GPU?⌄
Not currently. RunMat gathers GPU-resident inputs automatically and performs trimming on the CPU to maintain MATLAB compatibility.
Related Strings functions
Transform
erase · eraseBetween · extractBetween · join · lower · pad · replace · split · strcat · strjoin · strrep · strsplit · strtrim · upper
Core
char · compose · num2str · sprintf · str2double · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp
Search
contains · endsWith · startsWith · strfind
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strip is executed, line by line, in Rust.
- View the source for strip 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.