可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am retrieving a lot of informations in a list, linked to a database.
I want to create a string of groups, for someone who is connected to the website.
I use this to test ... But this is not dynamic so it is really bad:
string strgroupids = "6";
I want to use this now. But the string returned is something like 1,2,3,4,5,
groupIds.ForEach((g) =>
{
strgroupids = strgroupids + g.ToString() + ",";
strgroupids.TrimEnd(',');
});
strgroupids.TrimEnd(new char[] { ',' });
I want to delete the ,
after the 5
but it's definitely not working..
Can someone help me?
回答1:
strgroupids = strgroupids.Remove(strgroupids.Length - 1);
MSDN:
String.Remove(Int32):
Deletes all the characters from this string beginning at a specified
position and continuing through the last position
回答2:
What about doing it this way
strgroupids = string.Join( ",", groupIds );
A lot cleaner.
It will append all elements inside groupIds
with a ','
between each, but it will not put a ','
at the end.
回答3:
Strings in c# are immutable. When in your code you do strgroupids.TrimEnd(',');
or strgroupids.TrimEnd(new char[] { ',' });
the strgroupids
string is not modified.
You need to do something like strgroupids = strgroupids.TrimEnd(',');
instead.
To quote from here:
Strings are immutable--the contents of a string object cannot be
changed after the object is created, although the syntax makes it
appear as if you can do this. For example, when you write this code,
the compiler actually creates a new string object to hold the new
sequence of characters, and that new object is assigned to b. The
string "h" is then eligible for garbage collection.
回答4:
Add an extension method.
public static string RemoveLast(this string text, string character)
{
if(text.Length < 1) return text;
return text.Remove(text.ToString().LastIndexOf(character), character.Length);
}
then use:
yourString.RemoveLast(",");
回答5:
Removes any trailing commas:
while (strgroupids.EndsWith(","))
strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);
This is backwards though, you wrote the code that adds the comma in the first place. You should use string.Join(",",g)
instead, assuming g
is a string[]
. Give it a better name than g
too !
回答6:
As an alternate to adding a comma for each item you could just using String.Join:
var strgroupids = String.Join(",", groupIds);
This will add the seperator ("," in this instance) between each element in the array.
回答7:
string strgroupids = string.Empty;
groupIds.ForEach(g =>
{
strgroupids = strgroupids + g.ToString() + ",";
});
strgroupids = strgroupids.Substring(0, strgroupids.Length - 1);
Note that the use of ForEach
here is normally considered "wrong" (read for example http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx)
Using some LINQ:
string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => p + q + ',');
strgroupids = strgroupids.Substring(0, str1.Length - 1);
Without end-substringing:
string strgroupids = groupIds.Aggregate(string.Empty, (p, q) => (p != string.Empty ? p + "," + q : q.ToString()));
回答8:
Additional to sll's solution: It's better to trim the string in case there are some blank(s) at the end.
strgroupids = strgroupids.Remove(strgroupids.Trim().Length - 1);
回答9:
string.Join
is better, but if you really want a LINQ ForEach
:
var strgroupids = string.Empty;
groupIds.ForEach(g =>
{
if(strgroupids != string.Empty){
strgroupids += ",";
}
strgroupids += g;
});
Some notes:
string.Join
and foreach
are both better than this, vastly slower, approach
- No need to remove the last
,
since it's never appended
- The increment operator (
+=
) is handy for appending to strings
.ToString()
is unnecessary as it is called automatically when concatenating non-strings
- When handling large strings,
StringBuilder
should be considered instead of concatenating strings