How to remove a given word from a string in C#

1 Answer

0 votes
using System;

class Program
{
    public static string removeWord(string str, string word) {
        if (str.Contains(word)) {
            string tmp = word + " ";
            str = str.Replace(tmp, "");
            tmp = " " + word;
            str = str.Replace(tmp, "");
        }

        return str;
    }
    static void Main() {
        string str = "c# java c c++ python rust go";
        string word = "rust";

        str = removeWord(str, word);

        Console.Write(str);
    }
}




/*
run:
 
c# java c c++ python go
 
*/

 



answered Nov 19, 2022 by avibootz
...