开发者

replace the last word

开发者 https://www.devze.com 2023-03-01 13:35 出处:网络
i want to replace the last string as \"\".. Eg:-here the string is a开发者_StackOverflowpple and mango and

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号