How to remove the first occurrence of a word from a string in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        string s = "c c++ c# java python c# java";   
        string word = "c#";
        
        int index = s.IndexOf(word);
        
        s = (index < 0) ? s : s.Remove(index, word.Length + 1);

        Console.Write(s);
    }
}



/*
run:

c c++ java python c# java

*/

 



answered Apr 15, 2022 by avibootz
...