String.Join method that ignores empty strings?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
String.Join in C# does not skip empty or null strings by default — it includes them, producing doubled delimiters. To ignore empty strings, filter the input collection with LINQ's Where clause before passing it to String.Join. Use string.IsNullOrEmpty() to exclude empty strings, or string.IsNullOrWhiteSpace() to also exclude whitespace-only strings.
The Problem
The doubled commas look wrong in display text, CSV output, file paths, and addresses.
Fix: Filter with LINQ
Extension Method
Create a reusable extension for this common pattern:
Static Helper Method
If you prefer a static method matching String.Join's signature:
Building Addresses
A common use case — joining address parts where some fields may be empty:
Building File Paths
For file paths, Path.Combine is preferred over String.Join since it handles separators automatically.
Building SQL or CSV
For CSV, you usually want to preserve empty strings to maintain column alignment. Only filter when column order does not matter.
IsNullOrEmpty vs IsNullOrWhiteSpace
| Input | IsNullOrEmpty | IsNullOrWhiteSpace |
null | true | true |
"" | true | true |
" " | false | true |
"\t\n" | false | true |
"text" | false | false |
Common Pitfalls
- Forgetting to filter before joining:
String.Joinincludes all elements by default. Without aWhereclause, null values become empty strings in the output, producing doubled delimiters. - Using
IsNullOrEmptywhen whitespace should be excluded: Strings containing only spaces or tabs pass theIsNullOrEmptycheck. UseIsNullOrWhiteSpaceto filter those as well. - Filtering in CSV output: Removing empty strings from CSV rows shifts column positions. Only filter when column alignment does not matter, or use a proper CSV library that handles quoting and empty fields.
- Trimming strings after joining: Calling
.Trim()on the joined result removes leading/trailing whitespace from the whole string, not from individual parts. Trim each part before joining:.Select(s => s?.Trim()). - Performance with large collections: LINQ's
Wherecreates an iterator that is lazily evaluated. For very large collections, this is efficient because it does not allocate an intermediate array. However, calling.ToArray()beforeString.Joinforces a full allocation — letString.Joinconsume theIEnumerabledirectly.
Summary
String.Joindoes not skip empty or null strings — filter with LINQ first- Use
.Where(s => !string.IsNullOrEmpty(s))to exclude null and empty strings - Use
.Where(s => !string.IsNullOrWhiteSpace(s))to also exclude whitespace-only strings - Create an extension method (
JoinNonEmpty) for this common pattern - For CSV output, consider whether removing empty strings breaks column alignment
- Pass the filtered
IEnumerabledirectly toString.Join— no need for.ToArray()
Related reading
- String.Join vs. StringBuilder which is faster?
- String.Replace vs. StringBuilder.Replace
- String.Split only on first separator in C?
- string.ToLower and string.ToLowerInvariant
- Strip double quotes from a string in .NET
- Sum of digits in C
- Support Vector Machine library for C
- swagger .net core API ambiguous HTTP method for Action Error

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.