当前位置:  开发笔记 > 编程语言 > 正文

使用C#进行字符串连接

如何解决《使用C#进行字符串连接》经验,为你挑选了2个好方法。

我有一个输入字符串:

"风险管理,投资组合管理,投资规划"

如何将此字符串转换为:

"风险管理"+"投资组合管理"+"投资计划"

谢谢.



1> Konstantin T..:

拆分和修剪

 // include linq library like this:
 // using System.Linq;
 // then

"test1, test2".Split(',').Select(o => o.Trim());

要么

"test1, test2".Split(',').Select(o => o.Trim()).ToArray(); // returns array

"test1, test2".Split(',').Select(o => "\"" + o.Trim() + "\"")
.Aggregate((s1, s2) => s1 + " + " + s2);

// returns a string: "test1" + "test2"



2> Andrew Flana..:

使用Split()方法:

string[] phrases = s.Split(',');

现在你有一个每个逗号分隔值的字符串数组.

要删除空格,请Trim()在每个字符串上使用该方法(感谢John Feminella)

推荐阅读
Gbom2402851125
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有