删除名称前的字符串中的所有字符

avi*_*mer 0 c# string char

如何匹配特定名称之前,如何删除字符串中的所有字符?例如,我有以下字符串:

"C:\\Installer\\Installer\\bin\\Debug\\App_Data\\Mono\\etc\\mono\\2.0\\machine.config"
Run Code Online (Sandbox Code Playgroud)

如何在字符串' App_Data' 之前删除所有字符?

Ily*_*nov 6

var str = @"C:\Installer\Installer\bin\Debug\App_Data\Mono\etc\mono\2.0\machine.config";

var result = str.Substring(str.IndexOf("App_Data"));

Console.WriteLine(result);
Run Code Online (Sandbox Code Playgroud)

打印:

App_Data\Mono\etc\mono\2.0\machine.config
Run Code Online (Sandbox Code Playgroud)

好吧,这种花哨的方式是尝试使用平台无关的类Path,它设计用于处理文件和目录路径操作.在您的简单情况下,第一个解决方案在许多因素中更好,并仅考虑下一个解决方案作为示例

var result = str.Split(Path.DirectorySeparatorChar)
                .SkipWhile(directory => directory != "App_Data")
                .Aggregate((path, directory) => Path.Combine(path, directory));

Console.WriteLine(result); // will print the same
Run Code Online (Sandbox Code Playgroud)