remove a character from a string at a specific index in csharp

To remove a character from a string at a specific index in C#, you can use the Remove method of the StringBuilder class or the Substring method of the string class. Here's an example using Substring:

main.cs
string myString = "Hello, World!";
int indexToRemove = 5;
string newString = myString.Substring(0, indexToRemove) + myString.Substring(indexToRemove + 1);
155 chars
4 lines

In this example, we create a new string newString by concatenating the portion of myString before the index we want to remove and the portion after the index we want to remove.

Here's an example using StringBuilder:

main.cs
StringBuilder myStringBuilder = new StringBuilder("Hello, World!");
int indexToRemove = 5;
myStringBuilder.Remove(indexToRemove, 1);
string newString = myStringBuilder.ToString();
180 chars
5 lines

In this example, we remove the character at the specified index from the StringBuilder using the Remove method, and then convert the StringBuilder back to a string using the ToString method.

gistlibby LogSnag