Imagine you are the owner of a web site with a lot of comments (not like this one, where noone comments due to the fact that he should do it with his Facebook account 🙂 ). In this site there are people, who probably would love to share with you their opinion about the last derby match between <put any football derby here>. The point is that there are probably some words that you would like to avoid. What you can do, is to create a program, masking these words for you. Something like the following:
What have I done? I have hardcoded the input, in order to save place. Then for each word in the list of the “forbidden” words (in my case they are quite trivial and normal), I run a foreach loop, generating a string sPattern, with the size of the forbidden word and changing it, if it is present.
Enjoy the code 🙂 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using System.Text.RegularExpressions; class MAskOutWords { static void Main() { string sPattern = ""; string sInitial = "Vitosh is my name, football is my favourite game.\nWhat is your name? \nHow do you do? Are you from Sofia or from Berlin? Lalla - lala."; string sResult = sInitial; List<string> lToChange = new List<string>(new string[] { "my", "game", "is", "do", "Berlin", "Gotheborg", "JustSomethingNotAvailable", "lala" }); foreach (string item in lToChange) { sPattern = new string('*', item.Length); sResult = Regex.Replace(sInitial, item, sPattern); sInitial = sResult; } Console.WriteLine(sResult); } } |
🙂