How to remove a defined part of a string?

前端 未结 8 1873
死守一世寂寞
死守一世寂寞 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).

    0 讨论(0)
  • 2021-01-07 18:57
    Regex.Replace(@"NT-DOM-NV\MTA", @"(?:[^\\]+\\)?([^\\]+)", "$1")
    

    try it here.

    0 讨论(0)
提交回复
热议问题