String.Join method that ignores empty strings?

The VB.NET method String.Join(separator, stringArray) is similar to PHP's implode, but any null elements in the array are replaced with an empty string, so thatc:

Dim myArray() as String = { "a", null, "c" }
Console.WriteLine(String.Join(", ", myArray));
// Prints "a, , c"

Is there a simple way to concatenate a set of strings with a separator that ignores empty strings?

I don't necessarily need to use arrays or String.Join or anything else. I just need the following transformations:

("a", "b", "c") --> "a, b, c"
("a", null, "c") --> "a, c"

Solution 1:

VB.NET

String.Join(",", myArray.Where(Function(s) Not String.IsNullOrEmpty(s)))

C#

String.Join(",", myArray.Where(s => !string.IsNullOrEmpty(s)))

Solution 2:

for C# == > String.Join(",", arr.Where(s => !String.IsNullOrEmpty(s)));

Solution 3:

To do it in .NET 2.0 (no LINQ), e.g. for SQL-Server ReportingServices without having to write a function for it:

VB.NET

Dim a As String = "", b As String = "b", c As String = "", d As String = "d", e As String = ""
Dim lala As String = String.Join(" / ", String.Join(vbBack, New String() {a, b, c, d, e}).Split(New Char() {ControlChars.Back}, System.StringSplitOptions.RemoveEmptyEntries))

System.Console.WriteLine(lala)

C# (for those landing from google and not searching for VB.NET)

string a = "", b = "b", c = "", d = "d", e = "";
string lala = string.Join(" / ",
    string.Join("\u0008", 
        new string[] { a, b, c, d, e }
    ).Split(new char[] { '\u0008' }, System.StringSplitOptions.RemoveEmptyEntries)
);

System.Console.WriteLine(lala);

This assumes that the character backspace doesn't occur in your strings (should usually be true, because you can't simply enter this character by keyboard).

Also, if you get the values from a database, then it's even simpler, since you can do it in SQL directly:

PostgreSQL & MySQL:

SELECT 
    concat_ws(' / '
        , NULLIF(searchTerm1, '')
        , NULLIF(searchTerm2, '')
        , NULLIF(searchTerm3, '')
        , NULLIF(searchTerm4, '')
    ) AS RPT_SearchTerms; 

And even with the glorious MS-SQL-Server it's possible (PS: that's sarcasm):

DECLARE @in_SearchTerm1 nvarchar(100) 
DECLARE @in_SearchTerm2 nvarchar(100) 
DECLARE @in_SearchTerm3 nvarchar(100) 
DECLARE @in_SearchTerm4 nvarchar(100) 

SET @in_SearchTerm1 = N'a'
SET @in_SearchTerm2 = N''
SET @in_SearchTerm3 = N'c'
SET @in_SearchTerm4 = N''

SELECT 
    COALESCE
    (
        STUFF
        (
            (
                SELECT ' / ' + RPT_SearchTerm AS [text()]
                FROM 
                (
                                  SELECT NULLIF(@in_SearchTerm1, N'') AS RPT_SearchTerm, 1 AS RPT_Sort 
                        UNION ALL SELECT NULLIF(@in_SearchTerm2, N'') AS RPT_SearchTerm, 2 AS RPT_Sort  
                        UNION ALL SELECT NULLIF(@in_SearchTerm3, N'') AS RPT_SearchTerm, 3 AS RPT_Sort 
                        UNION ALL SELECT NULLIF(@in_SearchTerm4, N'') AS RPT_SearchTerm, 4 AS RPT_Sort 
                ) AS tempT 
                WHERE RPT_SearchTerm IS NOT NULL 
                ORDER BY RPT_Sort 
                FOR XML PATH(N''), TYPE 
            ).value('.', 'nvarchar(MAX)') 
            ,1
            ,3
            ,N''
        )
        ,N''
    ) AS RPT_SearchTerms