Reputation: 4182
I am retrieving a lot of information in a list, linked to a database and 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.
Upvotes: 345
Views: 635401
Reputation: 55
That code delete the last character in a string
string myString = "Hello;";
myString = myString.Remove(myString.Length-1);
Output
Hello
Upvotes: 1
Reputation: 3327
In C# 8 ranges and indices were introduced, giving us a new more succinct solution:
strgroupids = strgroupids[..^1];
Upvotes: 55
Reputation: 270
There is no "quick-and-dirty" way of doing this. I usually do:
mystring= string.Concat(mystring.Take(mystring.Length-1));
Upvotes: 1
Reputation:
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,
since it's never appended+=
) is handy for appending to strings.ToString()
is unnecessary as it is called automatically when concatenating non-stringsStringBuilder
should be considered instead of concatenating stringsUpvotes: 3
Reputation: 3307
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(",");
Upvotes: 15
Reputation: 302
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);
Upvotes: 5
Reputation: 111820
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()));
Upvotes: 3
Reputation: 8149
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.
Upvotes: 40
Reputation: 7121
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.
Upvotes: 4
Reputation: 60694
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.
Upvotes: 115
Reputation: 62484
strgroupids = strgroupids.Remove(strgroupids.Length - 1);
String.Remove(Int32):
Deletes all the characters from this string beginning at a specified position and continuing through the last position
Upvotes: 773
Reputation: 41983
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 !
Upvotes: 9