简体   繁体   中英

Convert multi-dimensional collection to string with separator

As I was working on this answer , I've written some code to convert a generic multi-dimensional collection to a string.

public static string ConvertToString<T>(this IEnumerable<IEnumerable<T>> input, string columnSplit = "", string rowSplit = "\n")
{
    return string.Join(rowSplit, input.Select(r => string.Concat(string.Join(columnSplit, r.Select(c => c.ToString())))));
}

Example Input

IEnumerable<IEnumerable<string>> input = new List<List<string>>
{
    new List<string> { "R", "L", "R", "R" },
    new List<string> { "L", "R", "V", "R" },
    new List<string> { "L", "R", "V", "R" },
    new List<string> { "R", "L", "L", "R" },
};

Desired Output

RLRR
LRVR
LRVR
RLLR

Although the code works, I don't find the solution to be elegant with the fact that it requires a string.Join inside a string.Concat inside a Select . Is there a way to simplify this solution.

this will work without the concat and the select....

public static string ConvertToString<T>(this IEnumerable<IEnumerable<T>> input, string columnSplit = "", string rowSplit = "\n")
{
  return string.Join(rowSplit, input.Select(r => string.Join(columnSplit, r)));
}

works with:

 IEnumerable<IEnumerable<string>> input = new List<List<string>>
            {
                new List<string> { "R", "L", "R", "R" },
                new List<string> { "L", "R", "V", "R" },
                new List<string> { "L", "R", "V", "R" },
                new List<string> { "R", "L", "L", "R" },
            };

and:

 IEnumerable<IEnumerable<int>> nums = new List<List<int>>
            {
                new List<int> { 1,2,3,4},
                new List<int> { 5,6,7,8},
            };

Since you're defining extensions anyway, why not go all the way?

public static string ConvertToString<T>(this IEnumerable<IEnumerable<T>> input, string columnSplit = "", string rowSplit = "\n") =>
    input.Select(r => r.ConvertToString(columnSplit)).ConvertToString(rowSplit);
    
public static string ConvertToString<T>(this IEnumerable<T> input, string split = "") => 
    string.Join(split, input.Select(i => i.ToString()));

These are some nasty methods I did

public static string ToStringEnumeration(this IEnumerable source)
{
    return ToStringEnumeration(source, EnumerationMode.NewLineEach, null);
}

public enum EnumerationMode
{
    NewLineEach,
    Separator,
}

public static string ToStringEnumeration(this IEnumerable source, EnumerationMode mode, string separator = null)
{
    return ToStringEnumeration(source, mode, separator, null, null);
}

public static string ToStringEnumeration(this IEnumerable source, EnumerationMode mode, string separator, IFormatProvider provider, string format)
{
    if (source is null)
        throw new ArgumentNullException(nameof(source));

    if (source.IsEmpty())
        return "";

    var sBuilder = new StringBuilder();
    foreach (var element in source)
    {
        var strElement = format is null ? element.ToString() : string.Format(provider, format, element);
        sBuilder.Append(strElement);

        if (mode == EnumerationMode.NewLineEach)
            sBuilder.AppendLine();
        else if (mode == EnumerationMode.Separator)
            sBuilder.Append(separator);
    }

    var sLength = separator?.Length ?? 1;

    if (mode == EnumerationMode.NewLineEach) //quit /n/r
        sBuilder.Remove(sBuilder.Length - 2, 2);
    else if (mode == EnumerationMode.Separator)
        sBuilder.Remove(sBuilder.Length - sLength, sLength);

    return sBuilder.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM