.net String.Join method that ignores empty strings?

64jmpszr  于 2022-12-14  发布在  .NET
关注(0)|答案(6)|浏览(171)

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

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

有没有一种简单的方法可以用忽略空字符串的分隔符连接一组字符串?
我不一定需要使用数组或String.Join或其他任何东西,我只需要以下转换:

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

py49o6xq1#

VB.NET

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

C#

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

6ss1mwsb2#

You can do it like this in CSharp:

String.Join(",", arr.Where(s => !String.IsNullOrEmpty(s)));
sz81bmfz

sz81bmfz3#

要在.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#(对于那些从谷歌登陆,而不是搜索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);

这里假设字符串中没有出现退格键(通常应该是真的,因为不能简单地通过键盘输入这个字符)。
此外,如果您从数据库中获取值,则会更加简单,因为您可以直接在SQL中执行此操作:
PostgreSQL和MySQL:

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

即使有了出色的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
34gzjxbg

34gzjxbg4#

请尝试以下操作:

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

prdp8dxp5#

另一种选择是使用LINQ方便的空过滤器进行过滤:

String.Join(",", myArray.OfType<string>())
kulphzqa

kulphzqa6#

这适用于VB.NET
Join(*yourArray*.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray(), *yourDelimiter*)

相关问题