39

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

Faulty Orc
  • 955
  • 4
  • 13
  • 24
  • 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. – Marcus Mangelsdorf Oct 06 '15 at 12:58

6 Answers6

69

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);
Kristoffer Lindvall
  • 2,674
  • 1
  • 18
  • 27
  • 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 '11 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. – David Heffernan Apr 18 '11 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. – Kristoffer Lindvall Apr 18 '11 at 10:34
  • mstrResult.Length -= 1; This works for me. cheers – Jagz W May 21 '13 at 04:56
15

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(",");
Md Nazmoon Noor
  • 3,187
  • 1
  • 24
  • 30
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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 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 '11 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 ? – spacemonkeys Apr 18 '11 at 10:08
  • Long live the Oxford comma! – goodeye Oct 06 '15 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);
Noctis
  • 11,507
  • 3
  • 43
  • 82
Aliostad
  • 80,612
  • 21
  • 160
  • 208
-2

Use:

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

then call

blahh.Remove( searchKey, 1).Insert(searchKey, " and"); or a blank.
Noctis
  • 11,507
  • 3
  • 43
  • 82