如何将 IEnumerable转换为到一个逗号分隔的字符串?

发布于 2024-12-05 11:27:10 字数 118 浏览 1 评论 0原文

假设出于调试目的,我想快速将 IEnumerable 的内容转换为一行字符串,每个字符串项以逗号分隔。我可以使用 foreach 循环在辅助方法中完成此操作,但这既不有趣也不简短。可以使用Linq吗?还有其他简短的方式吗?

Say that for debugging purposes, I want to quickly get the contents of an IEnumerable into one-line string with each string item comma-separated. I can do it in a helper method with a foreach loop, but that's neither fun nor brief. Can Linq be used? Some other short-ish way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嘿看小鸭子会跑 2024-12-12 11:27:10
using System;
using System.Collections.Generic;
using System.Linq;

class C
{
    public static void Main()
    {
        var a = new []{
            "First", "Second", "Third"
        };

        System.Console.Write(string.Join(",", a));

    }
}
using System;
using System.Collections.Generic;
using System.Linq;

class C
{
    public static void Main()
    {
        var a = new []{
            "First", "Second", "Third"
        };

        System.Console.Write(string.Join(",", a));

    }
}
蓝海似她心 2024-12-12 11:27:10
string output = String.Join(",", yourEnumerable);

String.Join 方法(字符串、IEnumerable

连接构造的 IEnumerable 集合的成员
类型 String,在每个成员之间使用指定的分隔符。

string output = String.Join(",", yourEnumerable);

String.Join Method (String, IEnumerable

Concatenates the members of a constructed IEnumerable collection of
type String, using the specified separator between each member.

长梦不多时 2024-12-12 11:27:10
collection.Aggregate("", (str, obj) => str + obj.ToString() + ",");
collection.Aggregate("", (str, obj) => str + obj.ToString() + ",");
辞取 2024-12-12 11:27:10

(a) 设置 IEnumerable:

// In this case we are using a list. You can also use an array etc..
List<string> items = new List<string>() { "WA01", "WA02", "WA03", "WA04", "WA01" };

(b) 将 IEnumerable 连接成字符串:

// Now let us join them all together:
string commaSeparatedString = String.Join(", ", items);

// This is the expected result: "WA01, WA02, WA03, WA04, WA01"

(c) 出于调试目的:

Console.WriteLine(commaSeparatedString);
Console.ReadLine();

(a) Set up the IEnumerable:

// In this case we are using a list. You can also use an array etc..
List<string> items = new List<string>() { "WA01", "WA02", "WA03", "WA04", "WA01" };

(b) Join the IEnumerable Together into a string:

// Now let us join them all together:
string commaSeparatedString = String.Join(", ", items);

// This is the expected result: "WA01, WA02, WA03, WA04, WA01"

(c) For Debugging Purposes:

Console.WriteLine(commaSeparatedString);
Console.ReadLine();
隔纱相望 2024-12-12 11:27:10
IEnumerable<string> foo = 
var result = string.Join( ",", foo );
IEnumerable<string> foo = 
var result = string.Join( ",", foo );
⊕婉儿 2024-12-12 11:27:10

要将一大串字符串连接成一个字符串,不要直接使用 +,而是使用 StringBuilder 逐一迭代,或者使用 String.Join 一举完成。

To join a large array of strings into a string, do not directly use +, rather use a StringBuilder to iterate one by one, or String.Join in one shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文