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"
The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.
join() The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
VB.NET
String.Join(",", myArray.Where(Function(s) Not String.IsNullOrEmpty(s)))
C#
String.Join(",", myArray.Where(s => !string.IsNullOrEmpty(s)))
for C# == > String.Join(",", arr.Where(s => !String.IsNullOrEmpty(s)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With