How to remove a defined part of a string?

前端 未结 8 2029
死守一世寂寞
死守一世寂寞 2021-01-07 18:19

I have this string: \"NT-DOM-NV\\MTA\" How can I delete the first part: \"NT-DOM-NV\\\" To have this as result: \"MTA\"

How is it possible with RegEx?

8条回答
  •  暖寄归人
    2021-01-07 18:51

    string.TrimStart(what_to_cut); // Will remove the what_to_cut from the string as long as the string starts with it.  
    

    "asdasdfghj".TrimStart("asd" ); will result in "fghj".
    "qwertyuiop".TrimStart("qwerty"); will result in "uiop".


    public static System.String CutStart(this System.String s, System.String what)
    {
        if (s.StartsWith(what))
            return s.Substring(what.Length);
        else
            return s;
    }
    

    "asdasdfghj".CutStart("asd" ); will now result in "asdfghj".
    "qwertyuiop".CutStart("qwerty"); will still result in "uiop".

提交回复
热议问题