Sabari M Sabari M
Updated date Dec 21, 2021
​In this blog, we will see how to remove string using Remove() method in the C# language.

The string Remove() method is used to remove characters in the string starting from the specified index or position and till the end of the string which means it removes all the strings after the specified index.

Syntax:

public string Remove(Int32 beginIndex)  

public string Remove(Int32 beginIndex, Int32 length)  

Example:

In this example, We have 2 methods to remove the characters from the string.

Method 1:

The string Remove() method uses one argument and removes characters from the 6th position till the end of the specified string (i.e., TechieClues).

// Using Remove method with one arguments. Remove all characters after an index.
string str = "TechieClues";
string result = str.Remove(6);
Console.WriteLine(result);

Output:

Techie

Method 2:

The string Remove() method uses two arguments and it removes characters or strings based on the range of the characters in the string.

// Using Remove method with two arguments. Remove range of characters in string.
string str1 = "TechieClues Developer Community Website";
int indx1 = str1.IndexOf(' ');
int indx2 = str1.IndexOf(' ', indx1 + 1);
string result1 = str1.Remove(indx1, indx2 - indx1);
Console.WriteLine(result1);

Output:

TechieClues Community Website

Entire program for reference:

class Program
{
	static void Main()
	{
		// Using Remove method with one arguments. Remove all characters after an index.
		string str = "TechieClues";
		string result = str.Remove(6);
		Console.WriteLine(result);

		// Using Remove method with two arguments. Remove range of characters in string.
		string str1 = "TechieClues Developer Community Website";
		int indx1 = str1.IndexOf(' ');
		int indx2 = str1.IndexOf(' ', indx1 + 1);
		string result1 = str1.Remove(indx1, indx2 - indx1);
		Console.WriteLine(result1);
		
		Console.ReadKey();
	}    
}

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!