Remove multiple words in a string using linq

Hi all,

Is there a way to find and remove multiple words in a string using linq?

For example i’d like to replace test1, test2 and test3 from the below from strTest if they appear

strTest = “This is a test1, test2 test to see if test3 words can be removed”

Thanks in advance

Hi,

I think we can achieve it using regex a the following.

System.Text.RegularExpressions.Regex.Replace(yourString,"\btest\d+\s*","")

Regards,

Thanks Yoichi, but the strings i’m looking to ignore could be ‘Dr’ or ‘Professor’ or teacher, etc. Is there anything that i can do to compare to a colleciton of strings & if any of those appear, remove?

How about the following expression?

keywords = {"Dr","Professor","teacher"}

Then

System.Text.RegularExpressions.Regex.Replace(yourString,"\b("+String.Join("|",keywords)+")\s*","",System.Text.RegularExpressions.RegexOptions.IgnoreCase)

Regards,

1 Like

Excellent! Thank you

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.