在C#中串联字符串时如何插入分隔符? [英] How do I insert a separator when concatenating a string in C#?
问题描述
我有一个字符串列表,并且通过使用此处在DotNetPerls上看到的方法,将其串连起来以整理列表,
I have a list of strings and I am concatenating it to flatten out the list by using the method seen here on DotNetPerls, http://www.dotnetperls.com/string-concat
我的问题是...在他们的清单示例中,其输出是"catdogperls"(请参见网页底部,就在摘要"之前)如何在"catdogperls"之间插入#号作为分隔符,从而它变成了"cat#dog#perls"?
My question is...in their List example where their output is "catdogperls" (see toward the bottom of the webpage, just before the Summary) how do I insert a # sign as a separator between "catdogperls" such that it becomes "cat#dog#perls"?
推荐答案
在这种情况下,您不想使用 string.Concat()
,您想
In this case you don't want to use string.Concat()
, you want to use string.Join()
. This accepts a separator and an array of strings to join by that separator. For example:
var joined = string.Join("#", theArray);
这会将字符串值放入 joined
:
"cat#dog#perls"
(当然,假设 theArray
包含这些值)
(assuming, of course, that theArray
contains those values)
这篇关于在C#中串联字符串时如何插入分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!