Trim last character from a string

后端 未结 14 1375
谎友^
谎友^ 2020-11-29 23:59

I have a string say

\"Hello! world!\" 

I want to do a trim or a remove to take out the ! off world but not off Hello.

14条回答
  •  -上瘾入骨i
    2020-11-30 00:35

    If you want to remove the '!' character from a specific expression("world" in your case), then you can use this regular expression

    string input = "Hello! world!";
    
    string output = Regex.Replace(input, "(world)!", "$1", RegexOptions.Multiline | RegexOptions.Singleline);
    
    // result: "Hello! world"
    

    the $1 special character contains all the matching "world" expressions, and it is used to replace the original "world!" expression

提交回复
热议问题