How to remove first and last character from string using C#.net?

In this tutorial, we will be demonstrating how to remove first and last character from a string in C#.NET with a few examples. We will also see, how to remove the first and last 2 characters from string c#.net.

A string is a sequence of characters. In C#, the string data type represents a string. String objects are immutable, meaning the value cannot be changed after it has been created. If you need to make changes to a string, like removing the first and last characters, a new string needs to be created.

string cityName = "Chicago";

Create C#.Net Method to Remove First and Last Character

Let’s define a method that will remove the first and last characters from a string.

public static string RemoveFirstAndLastChar(string input)
{
    if(input != null && input.Length > 2)
    {
        return input.Substring(1, input.Length - 2);
    }
    else
    {
        return "Input string is not long enough.";
    }
}

In this method:

  • We first check if the input string is not null and its length is more than 2. This is because if the string is null, or if it has fewer than 3 characters, it would not make sense to remove the first and last characters.
  • Substring(int startIndex, int length) is a method provided by the string class in C#. It returns a new string that is a substring of this string. The substring begins at a specified index and has a specified length. Here, we are starting at the second character (index 1) and taking the length of the string minus 2 to remove the last character.
  • If the input string is null or not long enough, we return an error message.

Example to Remove First and Last Character in C#.NET

Let us take another example to remove the first and the last character from a string in C#.Net

string[] cities = { "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio" };

foreach(string city in cities)
{
    string modifiedCity = RemoveFirstAndLastChar(city);
    Console.WriteLine("Original: " + city + " Modified: " + modifiedCity);
}

The output will come like below:

Original: Chicago Modified: hicago
Original: Houston Modified: ousto
Original: Phoenix Modified: hoeni
Original: Philadelphia Modified: hiladelphi
Original: San Antonio Modified: an Antoni

You can see the output below:

remove first and last character from string using c#
remove first and last character from string using c#

Remove the first and last 2 characters from string c#.net

Let us see an example of how to remove the first and last 2 characters from a string in C#.Net.

To remove characters from a string, C# provides the Substring method. It is one of the most common ways to manipulate strings. The Substring method returns a new string that starts at a specified character position and continues to the end of the string or a specified number of characters.

Here’s a simple way to remove the first and last two characters from a string in C#:

string originalString = "HelloWorld";
string newString = originalString.Substring(2, originalString.Length - 4);
Console.WriteLine(newString);

In the above code:

  • We first define the original string “United States of America“.
  • Then, we use the Substring method to create a new string. The Substring method takes two parameters:
    • The first parameter 2 indicates the start index for the new string. Remember, string index starts from 0.
    • The second parameter originalString.Length - 4 indicates the number of characters to be included from the original string in the new string. This is calculated by subtracting 4 (2 for the start and 2 for the end) from the length of the original string.
  • Finally, we output the new string, which should display ited States of Ameri.
remove first and last 2 characters from string c#
remove first and last 2 characters from string c#

Conclusion

In this C#.Net tutorial, we have understood, how to remove the first and last character from a string using C#.net. Also, we saw an example of, how to remove the first and last 2 characters from string c#.net.

You may also like: