TechieClues TechieClues
Updated date Aug 16, 2022
In this blog, we will see how to remove the first n characters from a string in C#. There are multiple methods available in C# to perform this.
  • 5.5k
  • 0
  • 0

In C#, you can easily remove any characters from the string using in-built methods. In this blog, we will see how to remove the first n characters from a string using 5 different methods.

  • Using String.Substring Method
  • Using String.Remove Method
  • Using LINQ Skip Method
  • Using String.ToCharArray Method
  • Using the Range Operator

Using String.Substring Method

We use String.Substring() method is used to obtain a substring of a string. In this example, we will use this method to remove the first 9 characters, The Substring() method will create a new string that starts at a specified position (for example, 9th char in this example) and continues till the end of the string.

class RemoveFirstNChars
{
	static void Main(string[] args)
	{
		string str = "World is beautiful";
		int n = 9;
		// Remove first 9 characters from the given string
		str = str.Substring(n);
		Console.WriteLine(str);
		Console.ReadKey();
	}
}  

// Output
//
// beautiful

Using String.Remove Method

The String.Remove() method is used to remove the specified range of characters from a string. In the below example, we remove the first 9 characters by passing the range from 0 to 9.

class RemoveFirstNChars
{
	static void Main(string[] args)
	{
		string str = "World is beautiful";
		int n = 9;
		// Remove first 9 characters from the given string
		str = str.Remove(0, n);
		Console.WriteLine(str);
		Console.ReadKey();
	}
}  

// Output
//
// beautiful

Using LINQ Skip Method

The SKIP method is used to skip the specified number of characters or elements and return the remaining characters or string. we can use String.Join() or String.Concat() as shown below,

class RemoveFirstNChars
{
	static void Main(string[] args)
	{
		string str = "World is beautiful";
		int n = 9;
		// Remove first 9 characters from the given string using String.Join
		str = String.Join(String.Empty, str.Skip(n));
		Console.WriteLine(str);

		string str1 = "Google search";
		int n1 = 7;
		// Remove first 7 characters from the given string using String.Concat
		str1 = String.Concat(str1.Skip(n1));
		Console.WriteLine(str1);

		Console.ReadKey();
	}

// Output
//
// beautiful
// search

Using String.ToCharArray Method

In the below example, we use String.ToCharArray() method to remove the first 9 characters as shown below,

class RemoveFirstNChars
{
	static void Main(string[] args)
	{
		string str = "World is beautiful";
		int n = 9;
		// Remove first 9 characters from the given string
		str = new string(str.ToCharArray(n, str.Length - n));
		Console.WriteLine(str);
		Console.ReadKey();
	}
}  

// Output
//
// beautiful

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!!!