TechieClues TechieClues
Updated date Jul 12, 2022
​In this blog, we will see how to remove string using Remove() method in the C# language.
  • 1.8k
  • 0
  • 0

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

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

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