Home > Blockchain >  Remove a specific character from string
Remove a specific character from string

Time:01-01

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

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?

CodePudding user response:

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

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

CodePudding user response:

C.Evenhuis has told you what you need to do to get the result you want, but I just wanted to point out why your first attempt didn't work..

Remove, in the way you used it, is something like a short way of saying RemoveEverythingAfter - it's used for cutting large parts out of strings, and is something like the the opposite of Substring. It isn't normally used for repeatedly removing individual characters or small parts. It cuts out and discards a section of a string, whereas Substring cuts out and keeps parts of a string

Think about string indexes as being between the letters:

 h e l l o _ w o r l d
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
0 1 2 3 4 5 6 7 8 9 1011

If you say Remove(7) it will remove everything from 7 onwards (towards the right, so, 8, 9 ...):

 h e l l o _ w o r l d
 ^^^^^^^^^^^^^ ^^^^^^^
    Keep        Remove


var hw = "hello_world";
Console.Print(hw.Remove(7)); //prints: hello_w

If you said Substring(7) it keeps everything from 7 onwards

 h e l l o _ w o r l d
 ^^^^^^^^^^^^^ ^^^^^^^
    Remove      Keep

var hw = "hello_world";
Console.Print(hw.Substring(7)); //prints: orld

Substring and Remove also have a form where they take a second parameter for the number of chars to keep or remove

If you said Substring(4, 3) it keeps a length of 3 from index 4, so keeping chars between 4 and 7. If you say Remove(4, 3) it removes 3 chars between 4 and 7

 h e l l o _ w o r l d
 ^^^^^^^ ^^^^^ ^^^^^^^               Substring(4, 3)
 Remove  Keep  Remove            --> result: o_w

 h e l l o _ w o r l d
 ^^^^^^^ ^^^^^ ^^^^^^^               Remove(4, 3)
 Keep    Remove  Keep            --> result: hellorld

All in, Remove wasn't really what you wanted, because it's for cutting out ranges, not all occurrences of one char/string

You could have used Remove repeatedly to remove one char at a time, while that char was still found in the string but it's incredibly tedious/low performance:
for(var idx = word.IndexOf(letter); idx > -1; idx = word.IndexOf(letter))
  word = word.Remove(idx, 1);

Footnote: Modern C# has another, more compact way of doing Substring just using indexes:

var hw = "hello_world";
hw[4..]  //keep from index 4 to end, i.e. o_world
hw[..4]  //keep from start to index 4, i.e. hell
hw[^4..] //keep from "4 back from end" to end, i.e. orld
hw[..^4] //keep from start to "4 back from end", i.e. hello_w
hw[7..10] //keep between 7 and 10, i.e. orl
hw[4..^4] //keep between 4 from start and 4 from end, i.e. o_w
hw[^8..8] //keep between 8 from end and 8 from start, i.e. lo_wo

And so on..

  •  Tags:  
  • c#
  • Related