Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete last char of string

Tags:

string

c#

char

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.

like image 236
Kiwimoisi Avatar asked Oct 26 '11 10:10

Kiwimoisi


People also ask

How do I remove the last character of a string?

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.

How do I remove the last 3 characters from a string?

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.

How do I remove the last character of a string in C++?

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.

How do I remove the last word from a string in Java?

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.


1 Answers

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

like image 169
sll Avatar answered Oct 25 '22 14:10

sll