Last post Feb 07, 2016 02:24 PM by mbanavige
Feb 03, 2016 07:43 PM|jjf184|LINK
In a C# MVC environment I have two strings. One contains a list of Noise words that are pipe delimited and I need to remove any of those words that occur in a string.
For instance strNoise ="Mr|MRS|DR|MS"
strSearch ="MR John Doe"
I need it to return "John Doe"
Feb 04, 2016 05:34 AM|Zhi Lv - MSFT|LINK
As for this issue, I suggest you could use LINQ Contains operator to check whether the words contains the noise words. Please refer to the following code:
string strNoise = "Mr|MRS|DR|MS";
string strSearch = "MR John Doe"; //Get the noise words list
List<string> noiselist = strNoise.ToLower().Split('|').AsEnumerable().ToList(); //Get the words which not contains the noise words
List<string> searchlist = strSearch.Split(' ').AsEnumerable().Where(c => !noiselist.Contains(c.ToLower())).ToList();
//Use the Join method to join the search words.
string outputSearch = string.Join(" ", searchlist);
Response.Write(outputSearch); //output: John Doe
More details about contains operator, please check these articles:
Feb 07, 2016 02:24 PM|mbanavige|LINK
You could also use regex
string strNoise = @"\b(Mr|MRS|DR|MS)\b";
string strSearch = "MR John Doe";
string outputSearch = Regex.Replace(strSearch, strNoise, "", RegexOptions.IgnoreCase).Trim();