在c#中字符串的开头和结尾添加逗号 [英] adding commas at beginning and end of string in c#

查看:942
本文介绍了在c#中字符串的开头和结尾添加逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个字符串变量,我必须concat.But之前我想检查每个字符串是否在开始和结束时都有逗号。如果它没有,那么在开始和结束时添加逗号,如果它有逗号,则删除它们我怎么做?

I have multiple string variables which i have to concat.But before that i want to check if every string has comma at start and end.If it doesnt have then add commas at start and end and if it has commas then remove them.How can i do this?

推荐答案

最好的方法是将它们全部删除,然后只将它们添加到你需要的地方:

The best way is to remove them all, and then only add them where you need them:
var rawStrings = myStringCollection.Select(s => s.Trim(','));
string concat = string.Join(",", rawStrings);

会这样做。


string replacement = stringvariable .replace(',','');



string concatenatdvalue =,+ replace +','
string replaced=stringvariable.replace(',','');

string concatenatdvalue=","+replaced+','


这篇关于在c#中字符串的开头和结尾添加逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆