strtrim — Remove leading and trailing whitespace from strings, character arrays, and cell arrays.
strtrim(text) removes leading and trailing whitespace characters from text. The input can be a string scalar, string array, character array, or a cell array of character vectors, mirroring MATLAB behaviour. Internal whitespace is preserved exactly as provided.
How strtrim works in RunMat
- Whitespace is defined via MATLAB's
isspace, so spaces, tabs, newlines, and other Unicode whitespace code points are removed from both ends of each element. - String scalars and arrays keep their type and shape. Missing string scalars (
<missing>) remain missing and are returned unchanged. - Character arrays are trimmed row by row. The result keeps the original number of rows and shrinks the column count to the longest trimmed row, padding shorter rows with spaces so the output stays rectangular.
- Cell arrays must contain string scalars or character vectors. Results preserve the original cell layout with each element trimmed.
- Numeric, logical, or structured inputs raise MATLAB-compatible type errors.
How strtrim runs on the GPU
strtrim runs on the CPU. When the input (or any nested element) resides on the GPU, RunMat gathers it to host memory before trimming so the output matches MATLAB exactly. Providers do not need to implement device kernels for this builtin today.
GPU memory and residency
You do not need to call gpuArray or gather manually. RunMat automatically gathers any GPU-resident text data before applying strtrim, so the builtin behaves the same regardless of where the data lives.
Examples
Trim Leading And Trailing Spaces From A String Scalar
name = " RunMat ";
clean = strtrim(name)Expected output:
clean = "RunMat"Remove Extra Whitespace From Each Element Of A String Array
labels = [" Alpha "; "Beta "; " Gamma"];
trimmed = strtrim(labels)Expected output:
trimmed = 3×1 string
"Alpha"
"Beta"
"Gamma"Trim Character Array Rows While Preserving Shape
animals = char(' cat ', 'dog', ' cow ');
result = strtrim(animals)Expected output:
result =
3×3 char array
'cat'
'dog'
'cow'Trim Tabs And Newlines Alongside Spaces
text = "\tMetrics " + newline;
clean = strtrim(text)Expected output:
clean = "Metrics"Trim Each Element Of A Cell Array Of Character Vectors
pieces = {' GPU ', " Accelerate", 'RunMat '};
out = strtrim(pieces)Expected output:
out = 1×3 cell array
{'GPU'} {"Accelerate"} {'RunMat'}Preserve Missing String Scalars
vals = [" ok "; "<missing>"; " trimmed "];
trimmed = strtrim(vals)Expected output:
trimmed = 1×3 string
"ok"
<missing>
"trimmed"FAQ
Does strtrim modify internal whitespace?
No. Only leading and trailing whitespace is removed; interior spacing remains intact.
Which characters count as whitespace?
strtrim removes code points that MATLAB's isspace recognises, including spaces, tabs, newlines, carriage returns, and many Unicode space separators.
How are character arrays resized?
Each row is trimmed independently. The output keeps the same number of rows and shrinks the width to match the longest trimmed row, padding shorter rows with spaces if necessary.
What happens to missing strings?
Missing string scalars (string(missing)) remain <missing> exactly as in MATLAB.
Can I pass numeric or logical arrays to strtrim?
No. Passing non-text inputs raises a MATLAB-compatible error indicating that text input is required.
How does strtrim differ from strip?
strtrim always removes leading and trailing whitespace. strip is newer and adds options for custom characters and directional trimming; use it when you need finer control.
What does strtrim do in MATLAB?
strtrim removes leading and trailing whitespace from a string or character array. Interior whitespace is preserved.
How is strtrim different from strip in MATLAB?
Both remove leading and trailing whitespace, but strip additionally lets you specify which side to trim ('left', 'right', or 'both') and which characters to remove.
Does strtrim work with string arrays?
Yes. strtrim operates element-wise on string arrays, removing whitespace from each element independently. Missing strings remain missing.
Related functions to explore
These functions work well alongside strtrim. Each page has runnable examples you can try in the browser.
strip, upper, lower, erase, eraseBetween, extractBetween, join, pad, replace, split, strcat, strrep
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strtrim works, line by line, in Rust.
- View strtrim.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.