C# program to remove first n characters from a string

C# program to remove first n characters from a string:

In this post, we will learn how to remove the first n characters from a string in C#. A string is a sequence of characters. We can access any character in a string by using the index. It starts from 0, i.e. the index of the first character is 0, second character is 1, etc.

String is immutable in C#, i.e. we can’t modify it. We need to create one new string if we are removing the first characters.

We can remove the first n characters from a string in two ways:

Method 1: By using String.Remove method:

Remove is a method defined in the String class as like below:

public string Remove(int start, int count)
  • It removes count number of characters from a string starting from the start index.
  • It returns one new string by removing the characters.
  • If we don’t provide count, it removes all strings from start to the end of the string.

Exception:

It can throw ArgumentOutOfRangeException if start is less than 0 or if count is less than 0. It will throw ArgumentOutOfRangeException if start and count refers an invalid position.

C# program:

So, we can use String.Remove to remove the first n characters from a string.

For example:

using System;
					
public class Program
{
	public static void Main()
	{
		string givenString = "Hello World";
		string newString = givenString.Remove(0, 3);
		
		Console.WriteLine(newString);
	}
}

It will print:

lo World

Method 2: By using String.Substring:

String.Substring method is used to get a substring from a string in C#. It is defined as like below:

public string Substring(int start, int size)

We can get a substring by providing the start index of the substring start and the length of the substring size. size is optional. If we don’t provide size, it will return the substring up to the end of the string.

It returns the modified string.

Exception:

It can throw ArgumentOutOfRangeException if start and size is less than zero, if start is invalid or it is greater than the size of the string or if start and size gives an invalid position.

C# program:

We can use this method to remove the first n characters from a string. The only thing we need to do is to pass the start value for Substring.

Below is the complete program:

using System;
					
public class Program
{
	public static void Main()
	{
		string givenString = "Hello World";
		string newString = givenString.Substring(3);
		
		Console.WriteLine(newString);
	}
}

In this program, we are removing the first 3 characters of the string or we are starting the substring from the 3rd index.

It will print:

lo World

Combining both:

using System;
					
public class Program
{
	public static void Main()
	{
		string givenString = "Hello World";
		
		string newString = givenString.Substring(3);
		string newString2 = givenString.Remove(0, 3);
		
		Console.WriteLine(newString);
		Console.WriteLine(newString2);
	}
}

C sharp remove first n character from a string

You might also like: