.net 忽略空字符串的 String.Join 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16325995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:14:04  来源:igfitidea点击:

String.Join method that ignores empty strings?

.netvb.netstring

提问by Doug

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:

VB.NET 的方法String.Join(separator, stringArray)类似于 PHP 的 implode,但是数组中的任何空元素都被替换为一个空字符串,这样c:

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:

我不一定需要使用数组或 String.Join 或其他任何东西。我只需要以下转换:

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

回答by Damith

VB.NET

网络

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

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

C#

C#

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

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

回答by SharpCoder

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

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

回答by Stefan Steiger

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

要在 .NET 2.0(无 LINQ)中执行此操作,例如对于 SQL-Server ReportingServices,而无需为其编写函数:

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)

C#(对于那些从谷歌登陆而不是搜索 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:

此外,如果您从数据库中获取值,那么它甚至更简单,因为您可以直接在 SQL 中执行此操作:

PostgreSQL & MySQL:

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):

即使使用了辉煌的 MS-SQL-Server,它也是可能的(PS:那是讽刺):

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 

回答by Stefan Steiger

Try the following:

请尝试以下操作:

var finalString = String.Join(",", ExampleArrayOfObjects.Where(x => !String.IsNullOrEmpty(x.TestParameter)).Select(x => x.TestParameter));

回答by user10642724

This works fine for VB.NET

这适用于VB.NET

Join(*yourArray*.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray(), *yourDelimiter*)

Join(*yourArray*.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray(), *yourDelimiter*)