C# program to remove the last n characters from a string

C# program to remove the last n characters from a string:

In this post, we will learn how to remove the last n characters from a string in C#. We have different ways to remove the last n characters from a string. A string is a sequence of characters in C#.

We can access any character in a string by using its index. The index starts from 0 and ends at length of the string - 1. i.e. the index of the first character is 0, 1 for the second character, 2 for the third character etc.

String is immutable in C#. i.e. we can’t change a string. We can’t remove characters from a string and modify that string. We need to create one new string. So, in these examples, whenever we are removing the last n characters from a string, we are actually creating a new string.

In this post, I will show you two different ways to do that.

Method 1: By using Remove:

String.Remove method is used to remove a specific numbers of characters from a string. This method can be used to remove the last n characters from a string. This method is defined as like below:

public string Remove(int start, int length)

Here, start is the starting index, from where we need to start removing the characters. length is the number of characters to remove from the string.

If we don’t provide length, it will remove all characters from start index to the end of the string.

Exception:

This method throws ArgumentOutOfRangeException,

  • if start or length is less than 0.
  • start and length points to an invalid position.

C# program:

So, to remove the last n characters from a string, we can provide start as the length - n and we don’t have to provide length. If we don’t provide length, it will take the substring from start index to the end of the string.

Below is the complete program:

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

It is using Remove to remove the last 3 characters of the string givenString. We are passing givenString.Length - 3 as the start index. So, it will remove the last 3 characters and return the substring holding the start characters.

If you run this program, it will return print the below output:

Hello wo

Method 2: By using Substring:

Substring is another method we can use here. This method is used to get a substring from a given string. It is defined as like below:

String.Substring is defined as like below:

Substring(int start, int length)

It returns a string from the start index of length size. length is optional. If we don’t provide this, it will return one substring from start to the end of the string.

Exception:

This method also throws ArgumentOutOfRangeException,

  • If start is less than zero or greater than the string size.
  • If start and length points to any invalid result.
  • If length is less than zero.

C# program:

We can use Substring to remove the last n characters of a string as like below:

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

Here,

  • We are providing the start index as 0.
  • The length as length of the string - 3.

It will remove the last n characters from the string givenString and return the substring.

If you run this program, it will print the below output:

Hello Wo

The last 3 characters are removed.

You might also like: