How to Remove First and Last 2 Characters From a String in C#.NET?

In this C#.Net tutorial, we will learn how to remove the first and last two characters from a string in C#.NET.

Remove First and Last 2 Characters From a String in C#.NET

C# provides a Substring() method, which is used to retrieve a substring from a string. It has two overloaded forms:

  1. Substring(int startIndex): This method returns a substring that begins at a specified position and continues to the end of the string.
  2. Substring(int startIndex, int length): This method returns a substring of a specified length, starting from a specified position in the string.

To remove the first and last two characters from a string in C#.net, we’ll use the Substring() method along with the Length property of the string. Here’s the general syntax:

string newString = oldString.Substring(2, oldString.Length - 4);

In this case, 2 is the starting index (since string indices start at 0, this means we’re skipping the first two characters), and oldString.Length - 4 is the length of the new string (we subtract 4 to remove two characters from the end as well).

Let’s look at some examples:

Example 1: Removing first and last two characters from the string “New York”

string cityName = "New York";
string newCityName = cityName.Substring(2, cityName.Length - 4);
Console.WriteLine(newCityName);  // Outputs: w Yo

Example 2: Removing first and last two characters from the string “Los Angeles”

string cityName = "Los Angeles";
string newCityName = cityName.Substring(2, cityName.Length - 4);
Console.WriteLine(newCityName);  // Outputs: s Angel

Example 3: Removing first and last two characters from the string “Chicago”

string cityName = "Chicago";
string newCityName = cityName.Substring(2, cityName.Length - 4);
Console.WriteLine(newCityName);  // Outputs: icag

In each of these examples, we’re creating a new string that contains all but the first and last two characters of the original city name.

Example 4: Complete example

using System;

class Program
{
    static void Main(string[] args)
    {
        string cityName = "Chicago";

        // Check if the string has at least 4 characters
        if(cityName.Length > 4)
        {
            string newCityName = cityName.Substring(2, cityName.Length - 4);
            Console.WriteLine(newCityName);  // Outputs: icag
        }
        else
        {
            Console.WriteLine("City name should have more than 4 characters");
        }
    }
}

In this program, we first import the System namespace, which provides fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions.

The Main function is the entry point of the program. In it, we create a string cityName with the value “Chicago”.

Then we have a conditional statement to check if the length of cityName is greater than 4. If so, we remove the first and last two characters using the Substring method and print the resulting string to the console.

If cityName has 4 or fewer characters, we print a message stating that the city name should have more than 4 characters.

Remember to use this conditional check in your code to avoid getting an ArgumentOutOfRangeException when working with the Substring method.

Check out the output below:

Remove First and Last 2 Characters From a String in C#.NET
Remove First and Last 2 Characters From a String in C#.NET

Conclusion

In this tutorial, you learned how to use the Substring method to remove the first and last two characters from a string in C#.NET. This is a handy technique for manipulating strings in a variety of applications.

Remember to always check the length of your string before using the Substring method to avoid ArgumentOutOfRangeException. The string should be long enough (at least 4 characters) so that after removing the first and last two characters, some characters should remain. Otherwise, you should add conditions to handle such cases.

You may also like: