remove a substring from a string in csharp

To remove a substring from a string in C#, you can use the Replace method of the string class. This method takes two parameters: the substring that you want to remove, and the string that you want to remove it from. It returns a new string with the substring removed.

Here's an example:

main.cs
string originalString = "Hello World";
string substringToRemove = "World";
string newString = originalString.Replace(substringToRemove, "");

Console.WriteLine(newString); // Output: Hello
189 chars
6 lines

In this example, we first create the original string "Hello World". We then specify the substring that we want to remove ("World") and use the Replace method to create a new string with that substring removed. The resulting string is "Hello", which we print to the console.

gistlibby LogSnag