C#:string[] 到分隔字符串。有单线吗?
我更喜欢的是这样的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于数组,您可以使用:
就我个人而言,我使用可以应用于所有类型的可枚举集合的扩展方法:
...我使用如下:
For arrays, you can use:
Personally, I use an extension method that I can apply to enumerable collections of all types:
...Which I use like so:
您可以使用静态 String.Join 方法:
String strNew = String.Join(chDelimiter, strArray);
编辑:回应评论:
根据您的评论,您可以获取多个数组,将它们连接在一起,然后连接整个结果数组。您可以使用 IEnumerable 扩展方法
Concat
来完成此操作。这是一个例子:希望这有帮助!
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:Hope this helps!
查看String.Join()。
您的样本必须如下所示:
Have a look at String.Join().
Your sample must look like this :
使用 String.Join
Use String.Join
在这种情况下, String.Join() 可能是最简单的方法,不过您也可以使用 LINQ
in this case, String.Join() is probably the easiest way to go, you can equally use LINQ though