40

I am having the following string builder as msrtResult, which is quite long:

mstrResult.Append(rtbResult.Text).Append("})})" + Environment.NewLine)

How can I remove the last "," from mstrResult Now? (it is in the middle of that mstrResult and it is not the last character of the whole string since I am appending strings to it) I should do it before adding the newline. Thanks

1
  • I, too, don't fully understand your question/goal. Where is that comma put into the StringBuilder? If it is contained in the rtbResult.Text you may consider replacing it there. Oct 6, 2015 at 12:58

6 Answers 6

68

You can just decrease the length to shorten the string (C#):

mstrResult.Length -= 1;

EDIT:

After you updated you question, I think I know what you want :) How about this:

mstrResult.Append(rtbResult.Text).Append("})})" + Environment.NewLine);
var index = mstrResult.ToString().LastIndexOf(',');
if (index >= 0)
    mstrResult.Remove(index, 1);
5
  • 1
    +1 I didn't think it would let you do this, but checked MSDN and yep it looks good. I like.
    – Buh Buh
    Apr 18, 2011 at 10:02
  • One thing to watch for is that you have added a command in the first place and that this code doesn't either fail due to the string not having any commas, or fails by removing a comma from an earlier portion of the text. Apr 18, 2011 at 10:15
  • @David You're right. Added a check to verify that there's really a comma present. I wasn't sure if @Faulty wanted to remove the comma from rtbResult of mstrResult, but changing that would be trivial. Apr 18, 2011 at 10:34
  • mstrResult.Length -= 1; This works for me. cheers
    – Jagz S
    May 21, 2013 at 4:56
  • The solution for C# should be through stringbuilder. Anything solution that doesn't mention stringbuilder is wrong IMHO. Yes, you can do it many other ways, but we should teach stringbuilder as the basics considering the exponential costs to not using it. Mar 31 at 20:59
16

Add a StringBuilder extension.

public static StringBuilder RemoveLast(this StringBuilder sb, string value)
{
    if(sb.Length < 1) return sb;
    sb.Remove(sb.ToString().LastIndexOf(value), value.Length);
    return sb;
}

then invoke:

yourStringBuilder.RemoveLast(",");
4

You can use StringBuilder.Remove() if you know the position of the character(s) you want to remove.

I'd imagine that it would be easier not to add it in the first place.


Your updated question talks about removing the last comma character. I'm guessing you want to do this to avoid creating a message that looks like this:

My shopping list contained milk, eggs, butter, and fish.

You'd assemble this in a loop, iterating over an array. Usually you can write the loop so that you simply choose (e.g. with an if statement) not to add the command when you are in the final iteration of the loop.

3
  • I do not know the position unfortuantely and also i can not do that because i am in an iteration and I just need to remove the last character of ","
    – Faulty Orc
    Apr 18, 2011 at 10:01
  • 2
    If you added the , at the start rather than at the end, you would know that it was the first one that need removing ... and then could you use Remove ? Apr 18, 2011 at 10:08
  • Long live the Oxford comma!
    – goodeye
    Oct 6, 2015 at 18:11
2

To just remove the last character you appended use below: (This is VB but C should work similarly)

Dim c As New StringBuilder
c.Remove(c.Length - 1, 1)
1

Try

mstrResult.Remove( mstrResult.Length - 1 - Environment.NewLine.Length
                 , Environment.NewLine.Length);
-2

Use:

stringbuilder blahh = new stringbuilder()
int searchKey = blahh.LastIndexOf(',');

then call

blahh.Remove( searchKey, 1).Insert(searchKey, " and"); or a blank.

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