How to remove a defined part of a string?

I have this string: “NT-DOM-NVMTA”
How can I delete the first part: “NT-DOM-NV”
To have this as result: “MTA”

How is it possible with RegEx?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);

Method 2

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);
        return s;

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

Method 3

Given that “” always appear in the string

var s = @"NT-DOM-NVMTA";
var r = s.Substring(s.IndexOf(@"") + 1);
// r now contains "MTA"

Method 4

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);

Method 5


string string1 = @"NT-DOM-NVMTA";
string string2 = @"NT-DOM-NV";

string result = string1.Replace( string2, "" );

Method 6

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-NVMTA";
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).

Method 7

 string s = @"NT-DOM-NVMTA";
 s = s.Substring(10,3);

Method 8

Regex.Replace(@"NT-DOM-NVMTA", @"(?:[^\]+\)?([^\]+)", "$1")

try it here.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x