C#:string[] 到分隔字符串。有单线吗?

发布于 2024-09-15 15:23:08 字数 349 浏览 5 评论 0原文

我更喜欢的是这样的:

string[] strArray = {"Hi", "how", "are", "you"};
string strNew = strArray.Delimit(chDelimiter);

但是,没有这样的功能。我查看了 MSDN,发现没有任何函数可以执行相同的操作。我查看了 StringBuilder,再次发现没有什么特别突出的地方。有谁知道有一种不太复杂的衬线可以使数组成为分隔字符串。谢谢你们的帮助。

更新:哇,哈哈,我的错。我一直看着数组本身的 .Join ,它让我烦透了。我什至没有看 String.Join。谢谢你们。一旦它允许我接受,我就会接受。感谢您的帮助。

What I'd prefer is something like:

string[] strArray = {"Hi", "how", "are", "you"};
string strNew = strArray.Delimit(chDelimiter);

However, there is no such function. I've looked over MSDN and nothing looked to me as a function that would perform the same action. I looked at StringBuilder, and again, nothing stood out to me. Does anyone know of a not to extremely complicated one liner to make an array a delimited string. Thanks for your guys' help.

UPDATE: Wow, lol, my bad. I kept looking at the .Join on the array itself and it was bugging the hell out of me. I didn't even look at String.Join. Thanks guys. Once it allows me to accept I shall. Preciate the help.

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

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

发布评论

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

评论(5

高速公鹿 2024-09-22 15:23:08

对于数组,您可以使用:

string.Join(", ", strArray);

就我个人而言,我使用可以应用于所有类型的可枚举集合的扩展方法:

public static string Flatten(this IEnumerable elems, string separator)
{
    if (elems == null)
    {
        return null;
    }

    StringBuilder sb = new StringBuilder();
    foreach (object elem in elems)
    {
        if (sb.Length > 0)
        {
            sb.Append(separator);
        }

        sb.Append(elem);
    }

    return sb.ToString();
}

...我使用如下:

strArray.Flatten(", ");

For arrays, you can use:

string.Join(", ", strArray);

Personally, I use an extension method that I can apply to enumerable collections of all types:

public static string Flatten(this IEnumerable elems, string separator)
{
    if (elems == null)
    {
        return null;
    }

    StringBuilder sb = new StringBuilder();
    foreach (object elem in elems)
    {
        if (sb.Length > 0)
        {
            sb.Append(separator);
        }

        sb.Append(elem);
    }

    return sb.ToString();
}

...Which I use like so:

strArray.Flatten(", ");
孤檠 2024-09-22 15:23:08

您可以使用静态 String.Join 方法:

String strNew = String.Join(chDelimiter, strArray);


编辑:回应评论
根据您的评论,您可以获取多个数组,将它们连接在一起,然后连接整个结果数组。您可以使用 IEnumerable 扩展方法 Concat 来完成此操作。这是一个例子:

//define my two arrays...
string[] strArray = { "Hi", "how", "are", "you" };
string[] strArray2 = { "Hola", "como", "esta", "usted" };

//Concatenate the two arrays together (forming a third array) and then call join on it...
string strNew = String.Join(",", strArray.Concat(strArray2));

希望这有帮助!

You can use the static String.Join method:

String strNew = String.Join(chDelimiter, strArray);


EDIT: In response to comment:
Based on your comment, you can take several arrays, concatenate them together, and then join the entire resulting array. You can do this by using the IEnumerable extension method Concat. Here's an example:

//define my two arrays...
string[] strArray = { "Hi", "how", "are", "you" };
string[] strArray2 = { "Hola", "como", "esta", "usted" };

//Concatenate the two arrays together (forming a third array) and then call join on it...
string strNew = String.Join(",", strArray.Concat(strArray2));

Hope this helps!

初见 2024-09-22 15:23:08

查看String.Join()

您的样本必须如下所示:

        string delimiter = ","
        string[] strArray = { "Hi", "how", "are", "you" };
        string strNew = String.Join(delimiter, strArray);

Have a look at String.Join().

Your sample must look like this :

        string delimiter = ","
        string[] strArray = { "Hi", "how", "are", "you" };
        string strNew = String.Join(delimiter, strArray);
浅浅 2024-09-22 15:23:08

使用 String.Join

string[] strArray = {"Hi", "how", "are", "you"};
string strNew = String.Join("," strArray);

Use String.Join

string[] strArray = {"Hi", "how", "are", "you"};
string strNew = String.Join("," strArray);
等数载,海棠开 2024-09-22 15:23:08

在这种情况下, String.Join() 可能是最简单的方法,不过您也可以使用 LINQ

var comSeparatedStrings = strings.Aggregate((acc, item) => acc + ", " + item);

in this case, String.Join() is probably the easiest way to go, you can equally use LINQ though

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