Remove Any Given Character From A String in C#

You have come to the correct spot if you’re looking for information on how to delete a certain character from a string. Read this blog and your issue will be resolved.

I now know what I’m going to do. First, I’ll assign lines of code to a string variable. Next, I’ll take a character variable and assign a value to the character you want to remove. Finally, I’ll find out the char value in the string again. Finally, I’ll use the string method to remove it from its current matched index.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeleteGivenCharacter {
    class Program {
        static void Main(string[] args) {
            string str = "Hello every one";
            char ch = 'e';
            Console.WriteLine("Before removing character:- {0}\n", str);
            for (int i = 0; i < str.Length - 1; i++) {
                if (str[i] == ch) {
                    str = str.Remove(i, 1);
                }
            }
            Console.WriteLine("After removing character '{0}' from string,\nfinal string is '{1}'", ch, str);
            Console.ReadKey();
        }
    }
}

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories