replace the last word
i want to replace the last string as ""..
Eg:-here the string is a开发者_StackOverflowpple and mango and the output should be apple and mango
if the last word is OR, NOT, AND i have to replace that to ""....
Thanks in advance....
Try using Regex.Replace
(in System.Text.RegularExpressions
)
Regex.Replace("This is the sentence AND", @"(AND|OR|NOT)\z", "");
\z
is a "end of the string"-anchor
You should also use string.Trim()
after that to trim excess spaces.
No need for regex:
string GetTrimmed(string str, string ending)
{
return str.EndsWith(ending) ?
str.Substring(0, str.Length - ending.Length) :
str;
}
Simple regex:
/( (and|or|not))$/i
Matches a space, followed by 'and', 'or' or 'not', at the end of the string. The i
after the closing regex marker tells it to be case insensitive.
You can match that word with following regex:
.*(\s+(or|not|and)\s*)$
You weren't exact about spaces in there, but the following regex should do it:
s/ \(and\|or\|not\) *$//gi
精彩评论