Alvin
Alvin

Reputation: 657

Which method to use to remove string characters in stringBuilder class

I would to know, Is there any method in StringBuilder class in C#, which can remove string characters without changing other character of same value within different index?

Like for a string like "5002", what if want to remove character in first index to "3"?

I'm using StringBuilder's remove method for the specified string and it's returning me an output as "5332" instead of "5302"?

The code which I'm using to accomplish my requirement is:

 StringBuilder j = new StringBuilder("5002");
 Console.WriteLine(j.Replace(j.ToString(1, 1),"3");

Upvotes: 1

Views: 867

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500775

Well, you can use the indexer:

builder[1] = '3';

Is that what you're after?

For example:

using System;
using System.Text;

class Test
{
    static void Main()
    {
        StringBuilder builder = new StringBuilder("5002");
        builder[1] = '3';
        Console.WriteLine(builder); // Prints 5302
    }
}

Upvotes: 2

Related Questions