windows 选择字符串的特定部分 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6778485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting a specific part of a string C#
提问by Adam Jones
I am trying to create a new string, which removes certain characters from an existing string e.g.
我正在尝试创建一个新字符串,它从现有字符串中删除某些字符,例如
string path = "C:\test.txt"
so string "pathminus" will take out the "C:\" e.g.
所以字符串“pathminus”将去掉“C:\”,例如
string pathminus = "test.txt"
回答by Chandu
回答by Filip Ekberg
There's so many ways that you can remove the a certain part of a string. These are a couple of ways to do it:
有很多方法可以删除字符串的某个部分。以下是几种方法:
var path = @"C:\test.txt";
var root = @"C:\";
Using string.Remove()
使用 string.Remove()
var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
Using string.Replace()
使用 string.Replace()
pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot); // prints test.txt
Using string.Substring()
使用 string.Substring()
pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot); // prints test.txt
Using Path.GetFileName()
使用 Path.GetFileName()
pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot); // prints test.txt
You can also use Regular Expression to find and replace parts of a string, this is a little bit harder though. You can read on MSDN on how to use Regular Expressions in C#.
您还可以使用正则表达式来查找和替换字符串的一部分,但这有点难。您可以在 MSDN 上阅读有关如何在 C# 中使用正则表达式的信息。
Here's a complete sample on how to use string.Remove()
, string.Replace()
, string.Substring()
, Path.GetFileName()
and Regex.Replace()
:
下面是关于如何使用一个完整的样本string.Remove()
,string.Replace()
,string.Substring()
,Path.GetFileName()
和Regex.Replace()
:
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
var path = @"C:\test.txt";
var root = @"C:\";
var pathWithoutRoot = path.Remove(0, root.Length);
Console.WriteLine(pathWithoutRoot);
pathWithoutRoot = Path.GetFileName(path);
Console.WriteLine(pathWithoutRoot);
pathWithoutRoot = path.Replace(root, "");
Console.WriteLine(pathWithoutRoot);
pathWithoutRoot = path.Substring(root.Length);
Console.WriteLine(pathWithoutRoot);
var pattern = "C:\\";
var regex = new Regex(pattern);
pathWithoutRoot = regex.Replace(path, "");
Console.WriteLine(pathWithoutRoot);
}
}
}
It all depends on what you want to do with the string, in this case you might want to remove just C:\
but next time you might want to do other types of operations with the string, maybe remove the tail or such, then the above different methods might help you out.
这完全取决于您想对字符串做什么,在这种情况下,您可能只想删除,C:\
但下次您可能想对字符串执行其他类型的操作,可能删除尾部等,然后上述不同的方法可能会帮助你。
回答by Corey Ogburn
The string class offers all sorts of ways to do this.
string 类提供了各种方法来做到这一点。
If you want to change "C:\test.txt" to "test.txt" by removing the first three characters:
如果您想通过删除前三个字符将“C:\test.txt”更改为“test.txt”:
path.Substring(3);
If you want to remove "C:\" from anywhere in the string:
如果要从字符串中的任何位置删除“C:\”:
path.Replace("C:\", "");
Or if you specifically want the filename, regardless of how long the path is:
或者,如果您特别想要文件名,无论路径有多长:
Path.GetFileName(path);
Depending on your intentions, there are many ways to do it. I prefer using the static class Path
.
根据您的意图,有很多方法可以做到。我更喜欢使用静态类Path
。
回答by Patrick
回答by Guffa
If the string is actually a file path, use the Path.GetFileName
method to get the file name part of it.
如果字符串实际上是文件路径,则使用该Path.GetFileName
方法获取其中的文件名部分。
回答by Rian Schmits
path.SubString(path.IndexOf('\'))
path.SubString(path.IndexOf('\'))
回答by Chris Hawkins
You want System.Text.RegularExpressions.Regex
but exactly what are you trying do here?
你想要,System.Text.RegularExpressions.Regex
但你到底想在这里做什么?
In its simplest form:
以其最简单的形式:
[TestMethod]
public void RemoveDriveFromPath()
{
string path = @"C:\test.txt";
Assert.AreEqual("test.txt", System.Text.RegularExpressions.Regex.Replace(path, @"^[A-Z]\:\", string.Empty));
}
Are you just trying to get the file name of a file without the path?
您是否只是想获取没有路径的文件的文件名?
If so do this instead:
如果是这样,请改为:
[TestMethod]
public void GetJustFileName()
{
string path = @"C:\test.txt";
var fileInfo = new FileInfo(path);
Assert.AreEqual("test.txt", fileInfo.Name);
}
回答by sbunny
For more general strings, use the string.Split(inputChar)
, which takes a character as a parameter and splits the string into a string[]
wherever that inputChar is found.
对于更一般的字符串,请使用string.Split(inputChar)
,它将一个字符作为参数并将字符串拆分string[]
为找到 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];