How to remove a defined part of a string?

前端 未结 8 2066
死守一世寂寞
死守一世寂寞 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:56

    You can use this extension method:

    public static String RemoveStart(this string s, string text)
    {
        return s.Substring(s.IndexOf(s) + text.Length, s.Length - text.Length);
    }
    

    In your case, you can use it as follows:

    string source = "NT-DOM-NV\MTA";
    string result = source.RemoveStart("NT-DOM-NV\"); // result = "MTA"
    

    Note: Do not use TrimStart method as it might trims one or more characters further (see here).

提交回复
热议问题