You may use the regular expression, but you have to take care of cases when your string starts or ends with the substring:
var pattern = @",?\bphani\b,?";
var regex = new Regex(pattern);
var result = regex.Replace(input, ",").Trim(',');
Shorter notation could look like this:
var result = Regex.Replace(input, @",?\bphani\b,?", ",").Trim(',');
Explanation of the regular expression: ,?\bphani\b,?
matches the word phani
, but only if preceded and followed by word-delimiter characters (because of the word boundary metacharacter \b
), and it can be (but doesn't have to be) preceded and followed by the comma thanks to ,?
which means none or more comma(s).
At the end we need to remove possible commas from the beginning and end of the string, that's why there's Trim(',')
on the result.
phani
is the last element in the string. You are better off using Regex to match and replace.