Trim last character from a string

后端 未结 14 1397
谎友^
谎友^ 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条回答
  •  孤独总比滥情好
    2020-11-30 00:36

    you could also use this:

    public static class Extensions
     {
    
            public static string RemovePrefix(this string o, string prefix)
            {
                if (prefix == null) return o;
                return !o.StartsWith(prefix) ? o : o.Remove(0, prefix.Length);
            }
    
            public static string RemoveSuffix(this string o, string suffix)
            {
                if(suffix == null) return o;
                return !o.EndsWith(suffix) ? o : o.Remove(o.Length - suffix.Length, suffix.Length);
            }
    
        }
    

提交回复
热议问题