选择字符串的特定部分C#
我正在尝试创建一个新字符串,它将从现有字符串中删除某些字符,例如选择字符串的特定部分C#,c#,.net,windows,C#,.net,Windows,我正在尝试创建一个新字符串,它将从现有字符串中删除某些字符,例如 string path = "C:\test.txt" 所以字符串“pathminus”将去掉“C:\”例如 你看过这个方法了吗?使用 例如: 如果字符串实际上是一个文件路径,请使用path.GetFileName方法获取其中的文件名部分。对于这个特定示例,我将研究该类。例如,您可以直接调用: string pathminus = Path.GetFileName(path); path.SubString(path.Inde
string path = "C:\test.txt"
所以字符串“pathminus”将去掉“C:\”例如
你看过这个方法了吗?使用
例如:
如果字符串实际上是一个文件路径,请使用
path.GetFileName
方法获取其中的文件名部分。对于这个特定示例,我将研究该类。例如,您可以直接调用:
string pathminus = Path.GetFileName(path);
path.SubString(path.IndexOf('\'))
string类提供了各种方法来实现这一点
如果要通过删除前三个字符将“C:\test.txt”更改为“test.txt”:
path.Substring(3);
如果要从字符串中的任何位置删除“C:\”:
path.Replace("C:\", "");
或者,如果您特别想要文件名,无论路径有多长:
Path.GetFileName(path);
根据你的意图,有很多方法可以做到这一点。我更喜欢使用静态类
Path
您想要的System.Text.RegularExpressions.Regex
,但您到底想在这里做什么
以最简单的形式:
[TestMethod]
public void RemoveDriveFromPath()
{
string path = @"C:\test.txt";
Assert.AreEqual("test.txt", System.Text.RegularExpressions.Regex.Replace(path, @"^[A-Z]\:\\", string.Empty));
}
您只是想获取没有路径的文件的文件名吗
如果是,则改为:
[TestMethod]
public void GetJustFileName()
{
string path = @"C:\test.txt";
var fileInfo = new FileInfo(path);
Assert.AreEqual("test.txt", fileInfo.Name);
}
有很多方法可以删除字符串的某个部分。以下是几种方法:
var path = @"C:\test.txt";
var root = @"C:\";
使用string.Remove()
var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot); // prints test.txt
使用string.Replace()
var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot); // prints test.txt
使用string.Substring()
var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot); // prints test.txt
使用Path.GetFileName()
var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot); // prints test.txt
您还可以使用正则表达式查找和替换字符串的部分,但这有点困难
下面是一个关于如何使用string.Remove()
,string.Replace()
,string.Substring()
,Path.GetFileName()
和Regex.Replace()
的完整示例:
这完全取决于您想对字符串执行什么操作,在这种情况下,您可能只想删除
C:\
,但下次您可能想对字符串执行其他类型的操作时,可以删除尾部之类的操作,那么上述不同的方法可能会帮助您解决问题。对于更一般的字符串,请使用string.Split(inputChar)
,它将字符作为参数,并将字符串拆分为字符串[]
,无论在哪里找到inputChar
string[] stringArr = path.Split('\\'); // need double \ because \ is an escape character
// you can now concatenate any part of the string array to get what you want.
// in this case, it's just the last piece
string pathminus = stringArr[stringArr.Length-1];