How to remove all occurrences of word from a string in C#

1 Answer

0 votes
using System;
 
public class RemoveAllOccurrencesOfWordFromString_CSharp
{
    public static string RemoveAllOccurrencesOfWordFromString(string s, string toremove) {
        s = s.Replace(" " + toremove + " ", " ");
        s = s.Replace(" " + toremove, "");
        s = s.Replace(toremove + " ", "");
        
        return s;
    }
    
    public static void Main(string[] args)
    {
        string s = "java c# rust java c c++ java java golang python java";
 
        s = RemoveAllOccurrencesOfWordFromString(s, "java");
        
        Console.WriteLine(s);   
    }
}
 
 
/*
run:
 
c# rust c c++ golang python
 
*/

 



answered Oct 13, 2024 by avibootz
edited Feb 2, 2025 by avibootz

Related questions

1 answer 167 views
1 answer 185 views
2 answers 130 views
2 answers 131 views
1 answer 116 views
...