strip — Remove leading and trailing characters from strings, character arrays, and cell arrays.
strip(text) removes consecutive whitespace characters from the beginning and end of text. The input can be a string scalar, string array, character array, or a cell array of character vectors, mirroring MATLAB behaviour. Optional arguments let you control which side to trim ('left', 'right', or 'both') and provide custom characters to remove instead of whitespace.
How does the strip function behave in MATLAB / RunMat?
- 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.
GPU behavior
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 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 of using strip in MATLAB / RunMat
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'}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.
See also
lower, upper, string, char, compose
Source & Feedback
- Source code: `crates/runmat-runtime/src/builtins/strings/transform/strip.rs`
- Found a bug? Open an issue with a minimal reproduction.