How to remove a defined part of a string?

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

    Try

    string string1 = @"NT-DOM-NV\MTA";
    string string2 = @"NT-DOM-NV\";
    
    string result = string1.Replace( string2, "" );
    
    0 讨论(0)
  • 2021-01-07 18:36

    Given that "\" always appear in the string

    var s = @"NT-DOM-NV\MTA";
    var r = s.Substring(s.IndexOf(@"\") + 1);
    // r now contains "MTA"
    
    0 讨论(0)
  • 2021-01-07 18:39

    If there is always only one backslash, use this:

    string result = yourString.Split('\\').Skip(1).FirstOrDefault();
    

    If there can be multiple and you only want to have the last part, use this:

    string result = yourString.SubString(yourString.LastIndexOf('\\') + 1);
    
    0 讨论(0)
  • 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".

    0 讨论(0)
  • 2021-01-07 18:54

    you can use this codes:

    str = str.Substring (10); // to remove the first 10 characters.
    str = str.Remove (0, 10); // to remove the first 10 characters
    str = str.Replace ("NT-DOM-NV\\", ""); // to replace the specific text with blank
    
    //  to delete anything before \
    
    int i = str.IndexOf('\\');
    if (i >= 0) str = str.SubString(i+1);
    
    0 讨论(0)
  • 2021-01-07 18:54
     string s = @"NT-DOM-NV\MTA";
     s = s.Substring(10,3);
    
    0 讨论(0)
提交回复
热议问题