1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/// <summary>
/// concatenates a list of strings into a single string (with a glue).
/// </summary>
/// <param name="source">source list</param>
/// <param name="glue">glue to concatenate the strings</param>
/// <returns>a single string</returns>
public static string Implode(this IEnumerable<string> source, string glue)
{
// exit criteria
if (source == null) throw new InvalidOperationException("source cannot be null");
if (source.Count() <= 0) return string.Empty;
// result with first item
var result = new StringBuilder(source.First());
// add glue & item for next items
foreach (string aPart in source.Skip(1))
{
result.Append(glue + aPart);
}
// return result
return result.ToString();
}
/// <summary>
/// removes all empty string from the source list
/// </summary>
/// <param name="source">source list with empty items</param>
public static IEnumerable<string> Trim(this IEnumerable<string> source)
{
// exit criteria
if (source == null) throw new InvalidOperationException("source cannot be null");
if (source.Count() <= 0) return source;
// filter all empty string
return from item in source where !string.IsNullOrEmpty(item) select item;
}
|