忽略空字符串的String.Join方法? [英] String.Join method that ignores empty strings?
问题描述
VB.NET方法 String.Join(separator,stringArray)
与PHP的爆破相似,但是数组中的任何null元素都将替换为空字符串,因此thatc:
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"
是否有一种简单的方法来连接a带有忽略空字符串的分隔符的字符串集?
Is there a simple way to concatenate a set of strings with a separator that ignores empty strings?
我不一定需要使用数组或String.Join或其他任何东西。我只需要进行以下转换:
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"
推荐答案
VB.NET
String.Join(,,myArray.Where(Function(s Not String.IsNullOrEmpty(s))))
C#
String.Join(,,myArray.Where(s =>!string.IsNullOrEmpty(s)))
这篇关于忽略空字符串的String.Join方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!