w3resource

C#: Remove specified a character from a non-empty string using index of a character

C# Sharp Basic: Exercise-15 with Solution

Write a C# program that removes a specified character from a non-empty string using the index of a character.

C# Sharp Exercises: Remove specified a character from a non-empty string using index of a character

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;

public class Exercise15 {
    // This is the main method where the program execution starts
    static void Main(string[] args)
    {
        // Displaying the result after removing a character at the specified index
        Console.WriteLine(remove_char("w3resource", 1)); // Removes character at index 1
        Console.WriteLine(remove_char("w3resource", 9)); // Removes character at index 9 (if exists)
        Console.WriteLine(remove_char("w3resource", 0)); // Removes character at index 0
    }

    // Function to remove a character at the specified index
    public static string remove_char(string str, int n)
    {
        return str.Remove(n, 1); // Using Remove method to eliminate the character at index n
    }
}

Test Data: w3resource

Sample Output:

wresource                                                              
w3resourc                                                              
3resource

Flowchart:

Flowchart: C# Sharp Exercises - Remove specified a character from a non-empty string using index of a character

C# Sharp Code Editor:

Previous: Write a C# Sharp program to convert from celsius degrees to Kelvin and Fahrenheit.
kelvin = celsius + 273

Next: Write a C# program to create a new string from a given string where the first and last characters will change their positions

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.