C# 从字符串中删除某些单词
您好,我正在使用GetDirectory获取程序的目录。有没有办法拆分以下内容?AGM\Program\Python1\bin\Debugs\已修复C# 从字符串中删除某些单词,c#,C#,您好,我正在使用GetDirectory获取程序的目录。有没有办法拆分以下内容?AGM\Program\Python1\bin\Debugs\已修复 C:\User\zhenhui\Desktop\AGM\Program\Python1\bin\Debug\ 我想要C:\User\zhenhui\Desktop\ C:\Users\zhenhui\Downloads\AGM\Program\Python1\bin\Debug\ 我想要C:\Users\zhenhui\Downloads\ C
C:\User\zhenhui\Desktop\AGM\Program\Python1\bin\Debug\
我想要C:\User\zhenhui\Desktop\
C:\Users\zhenhui\Downloads\AGM\Program\Python1\bin\Debug\
我想要C:\Users\zhenhui\Downloads\
C:\AGM\Program\Python1\bin\Debug\
我想要C:\
D:\AGM\Program\Python1\bin\Debug\
我要D:\
E:\AGM\Program\Python1\bin\Debug\
我想要E:\正如我在评论中提到的,因为您试图删除路径的固定部分,所以可以使用该方法。这是一个简单的控制台程序来演示,我创建了一个方法来返回修剪后的值,如果在非控制台应用程序中使用,则需要删除静态操作符
directory.substring(0, directory.indexOf("AGM\\Program\\Python1\\bin\\Debugs\"))
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(getProgramRootDirectory(@"C:\User\zhenhui\Desktop\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\Users\zhenhui\Downloads\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"D:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"E:\AGM\Program\Python1\bin\Debug\"));
Console.ReadLine();
}
static private string getProgramRootDirectory(string path)
{
return path.Replace(@"AGM\Program\Python1\bin\Debug\", "");
}
}
}
正如我在评论中提到的,由于您试图删除路径的固定部分,因此可以使用该方法。这是一个简单的控制台程序来演示,我创建了一个方法来返回修剪后的值,如果在非控制台应用程序中使用,则需要删除静态操作符
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(getProgramRootDirectory(@"C:\User\zhenhui\Desktop\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\Users\zhenhui\Downloads\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"D:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"E:\AGM\Program\Python1\bin\Debug\"));
Console.ReadLine();
}
static private string getProgramRootDirectory(string path)
{
return path.Replace(@"AGM\Program\Python1\bin\Debug\", "");
}
}
}
正如我在评论中提到的,由于您试图删除路径的固定部分,因此可以使用该方法。这是一个简单的控制台程序来演示,我创建了一个方法来返回修剪后的值,如果在非控制台应用程序中使用,则需要删除静态操作符
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(getProgramRootDirectory(@"C:\User\zhenhui\Desktop\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\Users\zhenhui\Downloads\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"D:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"E:\AGM\Program\Python1\bin\Debug\"));
Console.ReadLine();
}
static private string getProgramRootDirectory(string path)
{
return path.Replace(@"AGM\Program\Python1\bin\Debug\", "");
}
}
}
正如我在评论中提到的,由于您试图删除路径的固定部分,因此可以使用该方法。这是一个简单的控制台程序来演示,我创建了一个方法来返回修剪后的值,如果在非控制台应用程序中使用,则需要删除静态操作符
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(getProgramRootDirectory(@"C:\User\zhenhui\Desktop\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\Users\zhenhui\Downloads\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"C:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"D:\AGM\Program\Python1\bin\Debug\"));
Console.WriteLine(getProgramRootDirectory(@"E:\AGM\Program\Python1\bin\Debug\"));
Console.ReadLine();
}
static private string getProgramRootDirectory(string path)
{
return path.Replace(@"AGM\Program\Python1\bin\Debug\", "");
}
}
}
您看过regex或string.Split()吗?您还可以使用string.replace将固定字符串替换为空字符串。您看过regex或string.Split()吗?您还可以使用string.replace将固定字符串替换为空字符串。您看过regex或string.Split()吗?您还可以使用string.replace将固定字符串替换为空字符串。您看过正则表达式或string.Split()吗?您还可以使用string.replace将固定字符串替换为空字符串。几乎可以。您需要
子字符串(0,indexOf())
重载。现在您只需返回“AGM\\etc”。您可能需要执行int pos=directory.IndexOf(…)代码>后跟目录。子字符串(…)
ifpos!=-1
。差不多了。您需要子字符串(0,indexOf())
重载。现在您只需返回“AGM\\etc”。您可能需要执行int pos=directory.IndexOf(…)代码>后跟目录。子字符串(…)
ifpos!=-1
。差不多了。您需要子字符串(0,indexOf())
重载。现在您只需返回“AGM\\etc”。您可能需要执行int pos=directory.IndexOf(…)代码>后跟目录。子字符串(…)
ifpos!=-1
。差不多了。您需要子字符串(0,indexOf())
重载。现在您只需返回“AGM\\etc”。您可能需要执行int pos=directory.IndexOf(…)代码>后跟目录。子字符串(…)
ifpos!=-1
。