This is the Code for adding comma after another number, but I want to d开发者_高级运维elete the last comma:
str_MSISDN.Append("'" + _MSISDN[x].TrimStart() + "'" + ",");
Instead of manually appending things, I suggest you use String.Join
which will get it right to start with. You can use LINQ to do the trimming of the values. For example:
string x = string.Join(",", _MSISDN.Select(x => "'" + x.TrimStart() + "'")
.ToArray());
EDIT: A nicer version of this is available with MoreLINQ and its ToDelimitedString
method:
string x = _MSISDN.Select(x => "'" + x.TrimStart() + "'")
.ToDelimitedString(",");
You can do that using the TrimEnd
method (when you are done appending):
str_MSISDN = str_MSISDN.ToString().TrimEnd(',');
You can use the String.TrimEnd() method:
[your value] = str_MSISDN.ToString().TrimEnd(",".ToCharArray())
My favourite way of doing this kind of thing is to use the string.Join method:
string str_MSISDN = string.Join(", ", _MSISDN);
(assuming that _MSISDN is an array of strings)
If you want to trim the start of each item you can do this:
string str_MSISDN = string.Join(", ", _MSISDN.Select(x=>x.TrimStart()).ToArray());
Note how you have to call .ToArray since the Join method wants an array not an IEnumerable
I'd say don't add it in the first place, you've several options for doing this.
1) Use string.join
2) Restructure your loop as follows
int i = 0;
if (_MSISDN.Length > 0)
{
str_MSISDN.Append("'" + _MSISDN[i++].TrimStart() + "'")
while( i < _MSISDN.Length )
str_MSISDN.Append(",'" + _MSISDN[i++].TrimStart() + "'");
}
精彩评论