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.
The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.
slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.
Use pop_back() Function to Remove Last Character From the String in C++ The pop_back() is a built-in function in C++ STL that removes the last element from a string. It simply deletes the last element and adjusts the length of the string accordingly.
To remove the last word from a string, get the index of the last space in the string, using the lastIndexOf() method. Then use the substring() method to get a portion of the string with the last word removed.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With