ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Flatten

Flatten an IEnumerable<string>

Source

/// <summary>
/// Flattens an <see cref="IEnumerable"/> of <see cref="String"/> objects to a single string, seperated by an optional seperator and with optional head and tail.
/// </summary>
/// <param name="strings">The string objects to be flattened.</param>
/// <param name="seperator">The seperator to be used between each string object.</param>
/// <param name="head">The string to be used at the beginning of the flattened string. Defaulted to an empty string.</param>        
/// <param name="tail">The string to be used at the end of the flattened string. Defaulted to an empty string.</param>
/// <returns>Single string containing all elements seperated by the given seperator, with optionally a head and/or tail.</returns>
public static string Flatten(this IEnumerable<string> strings, string seperator, string head, string tail)
{
    // If the collection is null, or if it contains zero elements,
    // then return an empty string.
    if (strings == null || strings.Count() == 0)
        return String.Empty;

    // Build the flattened string
    var flattenedString = new StringBuilder();

    flattenedString.Append(head);
    foreach (var s in strings)
        flattenedString.AppendFormat("{0}{1}", s, seperator); // Add each element with the given seperator.
    flattenedString.Remove(flattenedString.Length - seperator.Length, seperator.Length); // Remove the last seperator
    flattenedString.Append(tail);

    // Return the flattened string
    return flattenedString.ToString();
}

/// <summary>
/// Flattens an <see cref="IEnumerable"/> of <see cref="String"/> objects to a single string with optional prefix and/or suffix for each string element.
/// </summary>
/// <param name="strings">The <see cref="IEnumerable"/> of <see cref="String"/> objects to flatten.</param>
/// <param name="prefix">String placed before each string element.</param>
/// <param name="suffix">String placed after each string element.</param>
/// <param name="head">The string to be used at the beginning of the flattened string. Defaulted to an empty string.</param>        
/// <param name="tail">The string to be used at the end of the flattened string. Defaulted to an empty string.</param>
/// <returns>Single string containing all elements with the given preifx and/or suffix and with optionally a head and/or tail.</returns>
public static string Flatten(this IEnumerable<string> strings, string prefix, string suffix, string head, string tail)
{
    // Return the flattened string
    return strings
        .Select(s => "{0}{1}{2}".WithFormat(prefix, s, suffix))
        .Flatten(String.Empty, head, tail);
}

Example

[Fact]
public void Can_Flatten_With_Seperator()
{
    // Arrange
    var list = Infrastructure.GetListOfStrings().Take(3);
    var expected = "monday; tuesday; wednesday";

    // Act
    var sut = list.Flatten("; ", "", "");

    // Assert
    Assert.Equal<string>(expected, sut);
}

[Fact]
public void Can_Flatten_With_Head_And_Tail()
{
    // Arrange
    var list = Infrastructure.GetListOfStrings().Take(3);
    var expected = "Days: monday, tuesday, wednesday.";

    // Act
    var sut = list.Flatten(", ", "Days: ", ".");

    // Assert
    Assert.Equal<string>(expected, sut);
}

[Fact]
public void Can_Flatten_With_Prefix_And_Suffix()
{
    // Arrange
    var list = Infrastructure.GetListOfStrings().Take(2);
    var expected = "<days><day>monday</day><day>tuesday</day></days>";

    // Act
    var sut = list.Flatten("<day>", "</day>", "<days>", "</days>");

    // Assert
    Assert.Equal<string>(expected, sut);
}

[Fact]
public void Returns_Empty_String_With_Empty_Source()
{
    // Arrange
    var list = Infrastructure.GetEmptyListOfStrings();
    var expected = String.Empty;

    // Act
    var sut = list.Flatten("; ", "head", "tail");

    // Assert
    Assert.Equal<string>(expected, sut);
}

[Fact]
public void Returns_Empty_String_When_Source_Is_Null()
{
    // Arrange
    var list = (List<string>)null;
    var expected = String.Empty;

    // Act
    var sut = list.Flatten("; ", "head", "tail");

    // Assert
    Assert.Equal<string>(expected, sut);
}

Author: Yves Schelpe (yves.schelpe@gmail.com)

Submitted on: 7 sep 2013

Language: C#

Type: System.IEnumerable<string>

Views: 6888