w3resource

C#: Remove the character in a given position of a given string


Remove Character at Position

Write a C# Sharp program to remove the character at a given position in the string. The given position will be in the range 0..(string length -1) inclusive.

Visual Presentation:

C# Sharp: Basic Algorithm Exercises - Remove the character in a given position of a given string.

Sample Solution:

C# Sharp Code:

using System;

// Namespace declaration
namespace exercises
{
    // Class declaration
    class Program
    {
        // Main method - entry point of the program
        static void Main(string[] args)
        {
            // Calling the 'test' method with different strings and integers and displaying the returned values
            Console.WriteLine(test("Python", 1));  // Output: "Pyton"
            Console.WriteLine(test("Python", 0));  // Output: "ython"
            Console.WriteLine(test("Python", 4));  // Output: "Pytho"
            Console.ReadLine();                   // Keeping the console window open
        }

        // Method to remove a character at a specified index 'n' from the input string 'str'
        public static string test(string str, int n)
        {
            return str.Remove(n, 1);  // Removing the character at index 'n' and returning the modified string
        }
   }
}

Sample Output:

Pthon
ython
Pythn

Flowchart:

C# Sharp: Flowchart: Remove the character in a given position of a given string.

For more Practice: Solve these Related Problems:

  • Write a C# program to remove every second character from a string starting from a given index.
  • Write a C# program to remove the middle character of a string only if its length is odd and greater than 1.
  • Write a C# program to remove characters from even positions in the string using a loop.
  • Write a C# program to delete all vowels from a given string using character positions.

Go to:


PREV : Add 'if' to String if Absent.
NEXT : Exchange First and Last Characters.

C# Sharp Code Editor:



Improve this sample solution and post your code through Disqus

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.