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

To remove a substring at a specific index in a C# string, you can use the Substring method to get the parts of the string that come before and after the substring you want to remove. Then, you can concatenate those parts back together to create the updated string.

Alternatively, you can use the Remove method, which takes the starting index and number of characters to remove as arguments. This is a bit more concise than using Substring, but may be less flexible if you need to remove a variable-length substring.

Here's an example of how to remove a substring at index 5 using either approach:

main.cs
string original = "hello world";
string substring = "o ";

// Using Substring:
string updated = original.Substring(0, 5) + original.Substring(5 + substring.Length);

// Using Remove:
updated = original.Remove(5, substring.Length);
231 chars
9 lines

In both cases, updated will be equal to the string "hellworld".

gistlibby LogSnag