我有一个字符串:“NT-DOM-NV\MTA”。 如何删除第一部分:“NT-DOM-NV”, 以获得以下结果:“MTA”?
您可以使用以下代码:
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);
假设字符串中总是出现 "\"
var s = @"NT-DOM-NV\MTA";
var r = s.Substring(s.IndexOf(@"\") + 1);
// r now contains "MTA"
@"NT-DOM-NV\MTA\ABC\file"
,并且在分割NT-DOM-NV
之后,我需要分割后的第一个字符串。在这种情况下,它应该是MTA
。如果我要分割NT-DOM-NV\MTA
,那么它应该返回ABC
。谢谢。 - ASNstring.TrimStart(what_to_cut); // Will remove the what_to_cut from the string as long as the string starts with it.
"asdasdfghj".TrimStart("asd" );
的结果为"fghj"
。
"qwertyuiop".TrimStart("qwerty");
的结果为"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" );
现在将会得到结果 "asdfghj"
。
"qwertyuiop".CutStart("qwerty");
仍然会得到结果 "uiop"
。
尝试
string string1 = @"NT-DOM-NV\MTA";
string string2 = @"NT-DOM-NV\";
string result = string1.Replace( string2, "" );
string result = yourString.Split('\\').Skip(1).FirstOrDefault();
string result = yourString.SubString(yourString.LastIndexOf('\\') + 1);
public static String RemoveStart(this string s, string text)
{
return s.Substring(s.IndexOf(s) + text.Length, s.Length - text.Length);
}
string source = "NT-DOM-NV\MTA";
string result = source.RemoveStart("NT-DOM-NV\"); // result = "MTA"
TrimStart
方法,因为它可能会进一步修剪一个或多个字符(请参见此处)。 string s = @"NT-DOM-NV\MTA";
s = s.Substring(10,3);