111

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 ?

1
  • 17
    Why do you have such a string? I assume because of a loop that concatenates commas to ints. You can use string.Join(",", ints) instead. That avoids trailing commas in the first place. Nov 27, 2013 at 15:32

9 Answers 9

320

Try string.TrimEnd():

Something = Something.TrimEnd(',');
4
  • 6
    This is the best solution because it works for all strings, including situations where you don't know ahead of time IF the string does in fact end in a comma, or not. Sep 27, 2017 at 21:38
  • 7
    This is why I still search for solutions even for things I could easily write myself. Someone else has usually solved it. :)
    – dpberry178
    Mar 21, 2019 at 20:23
  • 2
    So the single quotes weren't a mistake. Today I learnt char and string are defined with single and double quotations respectively.
    – egmfrs
    Dec 12, 2019 at 17:19
  • This is not the best solution. I created a line with multiple commas. Something.TrimEnd(','); removed all the commas instead of just the last one as the original question required. In the help tooltip it even states that it will remove all trailing characters matching the character(s) specified. However, it works well if your string always ends with one comma.
    – Fritz W
    Apr 20, 2022 at 6:35
21

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.

6

Try string.Remove();

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

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;
}
3
Dim psValue As String = "1,5,12,34,123,12"
psValue = psValue.Substring(0, psValue.LastIndexOf(","))

output:

1,5,12,34,123
2
  • Question was about C# not VB. Oct 26, 2018 at 8:20
  • 2
    Answer was in VB but same for C# so it helped me. Jan 25, 2022 at 13:44
2

Try below

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

1

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

1

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

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

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

1
  • Won't this error if "Something" is an empty string?
    – KeyOfJ
    Jul 27, 2021 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.