Remove last specific character in a string c#

C#Trim

C# Problem Overview


I use WinForms c#.I have string value like below,

string Something = "1,5,12,34,";

I need to remove last comma in a string. So How can i delete it ?

C# Solutions


Solution 1 - C#

Try string.TrimEnd():

Something = Something.TrimEnd(',');

Solution 2 - C#

King King's answer is of course correct, and Tim Schmelter's comment is also good suggestion in your case.

But if you really want to remove the last comma in a string, you should find the index of the last comma and remove it like this:

string s = "1,5,12,34,12345";
int index = s.LastIndexOf(',');
Console.WriteLine(s.Remove(index, 1));

Output will be:

1,5,12,3412345

Here is a demonstration.

It is unlikely that you want this way but I want to point it out. And remember, the String.Remove method doesn't remove any characters in the original string, it returns new string.

Solution 3 - C#

Try string.Remove();

string str = "1,5,12,34,";
string removecomma = str.Remove(str.Length-1);
MessageBox.Show(removecomma);

Solution 4 - C#

The TrimEnd method takes an input character array and not a string. The code below from Dot Net Perls, shows a more efficient example of how to perform the same functionality as TrimEnd.

static string TrimTrailingChars(string value)
{
    int removeLength = 0;
    for (int i = value.Length - 1; i >= 0; i--)
    {
	    char let = value[i];
	    if (let == '?' || let == '!' || let == '.')
	    {
	        removeLength++;
	    }
	    else
	    {
	        break;
	    }
    }
    if (removeLength > 0)
    {
	    return value.Substring(0, value.Length - removeLength);
    }
    return value;
}

Solution 5 - C#

Dim psValue As String = "1,5,12,34,123,12"
psValue = psValue.Substring(0, psValue.LastIndexOf(","))

output:

1,5,12,34,123

Solution 6 - C#

Try below

Something..TrimEnd(",".ToCharArray());

Solution 7 - C#

Or you can convert it into Char Array first by:

string Something = "1,5,12,34,";
char[] SomeGoodThing=Something.ToCharArray[];

Now you have each character indexed:

SomeGoodThing[0] -> '1'
SomeGoodThing[1] -> ','

Play around it

Solution 8 - C#

When you have spaces at the end. you can use beliow.

ProcessStr = ProcessStr.Replace(" ", "");
Emails     = ProcessStr.TrimEnd(';');

Solution 9 - C#

Try this, string Something1= Something.Substring(0, Something.Length - 1 );

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3042186View Question on Stackoverflow
Solution 1 - C#King KingView Answer on Stackoverflow
Solution 2 - C#Soner GönülView Answer on Stackoverflow
Solution 3 - C#user2509901View Answer on Stackoverflow
Solution 4 - C#bayuView Answer on Stackoverflow
Solution 5 - C#nambi_r88View Answer on Stackoverflow
Solution 6 - C#pmhView Answer on Stackoverflow
Solution 7 - C#Faisal AshfaqView Answer on Stackoverflow
Solution 8 - C#Lahiru GamageView Answer on Stackoverflow
Solution 9 - C#gudlyfView Answer on Stackoverflow