How To Remove Any Given Character From A String Using C#

You have come to the right place if you’re looking for information on how to remove 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 from its current matched index.

 

Let us write code here,

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();
        }
    }
}

 

 

There is the output:

 

I sincerely hope you enjoy it; if you have any questions, please leave them in the comment section.

 

Submit a Comment

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

Subscribe

Select Categories