I want to use Regex.Replace() to loop through a string containing words that are separated by '//' to check if any of the words match a string value which has been passed to the method. If the text string does match one of the words in the wordList, replace it and return 'matched', if it doesn't match any of the words then return the original word that was passed to the method and don't replace it.
Here's my code at the moment:
public void CheckText(string text)
{
//Check text entered by user
string wordList = "word1//word2//word3 etc...";
string replaceString = "matched";
if (!string.IsNullOrEmpty(wordList))
{
//How do I implement this part?
return Regex.Replace(text, wordList, replaceString);
}
}
Please could someone help me with this? Any help/comments will be appreciated!
Update: (pasted from update to question posted as an answer by OP)
Thanks for your replies. I probably didn't explain the question correctly. I want the method to replace the text string it is passed if it matches a string in the wordList. For example, 'word1' is passed to the method, the method then checks to see if 'word1' is in the wordList and if it is, replace the original string that was passed to the method with 'matched', and then return 'matched', if it didn't match any of the words in the wordList, then return the orignial string and don't replace it.
voidimplies you don't want to return anything, the method nameCheckTextsuggests you want to return a Boolean value and return line suggests you want to return a string value (fromRegex.Replace). Maybe clarify what you're trying to achieve and we can help you better.