How to remove a defined part of a string?
108,864
Solution 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);
Solution 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);
else
return s;
}
"asdasdfghj".CutStart("asd" );
will now result in "asdfghj"
.
"qwertyuiop".CutStart("qwerty");
will still result in "uiop"
.
Solution 3
Given that "\" always appear in the string
var s = @"NT-DOM-NV\MTA";
var r = s.Substring(s.IndexOf(@"\") + 1);
// r now contains "MTA"
Solution 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);
Solution 5
Try
string string1 = @"NT-DOM-NV\MTA";
string string2 = @"NT-DOM-NV\";
string result = string1.Replace( string2, "" );
Author by
Tassisto
Specialized in software developing using .NET Technologies.
Updated on June 29, 2020Comments
-
Tassisto almost 4 years
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?
-
Øyvind Bråthen about 13 yearsI think the point is for this to work on several different strings, so that he has two strings, and want to remove one from the other.
-
Daniel Hilgarth about 13 years-1: He never specified, that either one of the parts is of fixed length.
-
Paolo Falabella about 13 years@Daniel: to be fair he has not specified much at all... There may not be a "\" either
-
Daniel Hilgarth about 13 years@paolo: You are right. But the assumption that any of those strings is of fixed length maybe would have been correct 30 years ago but not nowadays.
-
Tassisto about 13 yearsthis is just what I needed! Thank you.
-
Fun Mun Pieng about 13 years@Tassisto, I believe I have more than enough examples to satisfy what you need. If none of them address the issue at hand, please explain again what you need. Hope I have been helpful.
-
agradl about 13 yearsHe gave the exact string as well as the desired result...not sure what the big deal is
-
katta over 9 yearsstring result = string1.Replace( string2, string.Empty );
-
ASN almost 8 yearsWhat if the string is
@"NT-DOM-NV\MTA\ABC\file"
and after splittingNT-DOM-NV
, I need the first string after split. In this case it should beMTA
. If I have to splitNT-DOM-NV\MTA
then it should returnABC
. TIA -
Jacob over 6 yearsWired issue. I think many developers don't know that. Anyway, it is really good and clear solution, I replace ALL TrimStart in our application to your CutStart method. Thank you.
-
AliNajafZadeh over 2 yearsThe purpose of the question is to search for and delete a string from the beginning of another string. With your method you can not dynamically identify the location of the string and delete it.
-
Mikael Östberg over 2 years@zambee The purpose of the question is the get whatever is after the \ char, which is solved in my answer.
-
AliNajafZadeh over 2 years@Mikael Östberg OK. Thanks.