Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove a specific character from string

I have an application

namespace Aplikacija1
{
    public class excercise5
    {
        static void Main(string[] args)
        {         
            Console.WriteLine("Enter word :");
            string word = Console.ReadLine();
            Console.WriteLine("Enter the letter :");
            string letter = Console.ReadLine();
            int charPos = word.IndexOf($"{letter}");
            Console.WriteLine(word.Remove(charPos, 1));
        }
    }
} 

so it should remove a letter from the string which is entered.

If I enter word Nikola and letter a the result is Nikol which is correct

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But if I enter word Nikala and the letter a the result is Nikla, with no option to remove second a, or choose which a I want to remove

Is there a way to add something to my code to make it work?

>Solution :

You could use the Replace() method and replace it with an empty string:

string wordWithoutLetter = word.Replace(letter, "");
Console.WriteLine(wordWithoutLetter);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading