w3resource

C#: Remove the first and last elements from a given string


Remove First and Last Characters

Write a C# Sharp program to remove the first and last elements from a given string.

Sample Solution:

C# Sharp Code:

using System;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            // Displays original strings and the result after removing first and last elements
            Console.WriteLine("Original string: PHP");
            Console.WriteLine("After removing first and last elements: " + test("PHP"));

            Console.WriteLine("Original string: Python");
            Console.WriteLine("After removing first and last elements: " + test("Python"));

            Console.WriteLine("Original string: JavaScript");
            Console.WriteLine("After removing first and last elements: " + test("JavaScript"));
        }

        // Function to remove the first and last characters of a string
        public static string test(string str1)
        {
            // Using conditional (ternary) operator to check if string length is greater than 2
            // If string length is greater than 2, it returns the substring excluding the first and last characters
            // If string length is less than or equal to 2, it returns the original string
            return str1.Length > 2 ? str1.Substring(1, str1.Length - 2) : str1;
        }
    }
}

Sample Output:

Original string: PHP
After removing first and last elements: H
Original string: Python
After removing first and last elements: ytho
Original string: JavaScript
After removing first and last elements: avaScrip

Flowchart:

Flowchart: C# Sharp Exercises - Remove the first and last elements from a given string.

Sample Solution-1:

C# Sharp Code:

using System;

namespace exercises
{
    class Program
    {
        // Main method where the program execution begins
        static void Main(string[] args)
        {
            // Displays original strings and the result after removing first and last elements
            Console.WriteLine("Original string: PHP");
            Console.WriteLine("After removing first and last elements: " + test("PHP"));

            Console.WriteLine("Original string: Python");
            Console.WriteLine("After removing first and last elements: " + test("Python"));

            Console.WriteLine("Original string: JavaScript");
            Console.WriteLine("After removing first and last elements: " + test("JavaScript"));
        }

        // Function to remove the first and last characters of a string
        public static string test(string str1)
        {
            // Using conditional (ternary) operator to check if string length is less than or equal to 2
            // If string length is less than or equal to 2, it returns the original string
            // If string length is greater than 2, it returns the substring excluding the first and last characters
            return str1.Length <= 2 ? str1 : str1.Substring(1, str1.Length - 2);
        }
    }
}

Sample Output:

Original string: PHP
After removing first and last elements: H
Original string: Python
After removing first and last elements: ytho
Original string: JavaScript
After removing first and last elements: avaScrip

Flowchart:

Flowchart: C# Sharp Exercises -Remove the first and last elements from a given string.

For more Practice: Solve these Related Problems:

  • Write a C# program to remove the second and second-to-last characters from a string.
  • Write a C# program to remove characters at even index positions from a string.
  • Write a C# program to remove the first and last word from a sentence string.
  • Write a C# program to remove the first three and last three characters from a given string if it is long enough.

Go to:


PREV : Check All Uppercase or Lowercase.
NEXT : Check Consecutive Similar Letters.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.