trim all strings in an array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Trimming every string in an array is a small cleanup step that prevents larger data-quality problems later. Leading and trailing whitespace can break comparisons, create duplicate-looking values, and make UI output inconsistent. The right implementation should be clear about whether it mutates the array, how it handles null values, and whether empty results should be kept.
JavaScript: Use map with trim
In JavaScript, the most direct approach is to create a new array with map.
This preserves array length and leaves the original array unchanged.
If the input may contain null or undefined, guard explicitly:
That is safer than blindly calling trim on every element.
Python: List Comprehension
Python has the same pattern with strip, which removes leading and trailing whitespace.
If you want to skip None values or preserve them, decide that rule explicitly.
The important thing is to avoid letting cleanup rules stay implicit.
C#: Select or In-Place Update
In C#, a LINQ projection is the most readable non-mutating option.
If null values are possible:
If you really want to mutate an existing array:
Mutation is fine when the array is local and the behavior is obvious, but creating a new array is often safer in shared code.
Decide Whether to Keep Empty Strings
After trimming, some values may become empty. That raises a second policy question: should empty strings remain, or should they be removed.
JavaScript example:
Python version:
Trimming and filtering are related, but they are not the same operation. Keeping those steps separate makes intent clearer.
Build Reusable Cleanup Helpers
If trimming arrays is a repeated pattern in the codebase, wrap it in a helper with a documented rule set.
Or in JavaScript:
This prevents slightly different cleanup rules from spreading across the project.
Think About Input Boundaries
Trimming is usually best done at system boundaries:
- after reading CSV or JSON input
- after accepting form fields
- before comparing identifiers or names
- before storing user-visible values
If you normalize early, downstream logic becomes simpler because it can assume cleaner input.
Common Pitfalls
The most common mistake is assuming every array element is a string. If the array can contain nulls or mixed types, direct calls to trim or strip will fail.
Another issue is mutating shared arrays without meaning to. That can produce surprising behavior if other code still expects the original values.
Teams also sometimes collapse trimming and filtering into one dense expression that hides business rules. If empty strings need special handling, make that rule explicit.
Summary
- Trim arrays of strings with
map, list comprehensions, orSelect. - Decide up front whether the operation should mutate or return a new collection.
- Handle null or mixed-type elements explicitly instead of assuming every value is a string.
- Treat trimming and filtering as separate decisions.
- Normalize input near system boundaries so later code can stay simpler.
Related reading
- Triplet whose sum in range 1,2
- Two-dimensional array in Swift
- Two-way / bidirectional Dictionary in C?
- Two elements in array whose xor is maximum
- Try-catch speeding up my code?
- Try-catch speeding up my code?
- Type List vs type ArrayList in Java
- type mismatch error, expected type LIST for querying a one-to-many relationship in AppSync

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.