当有多个字符时,如何使用 IndexOf 来选择特定字符?
当有多个字符时,如何使用 IndexOf 和 SubString 来选择特定字符?这是我的问题。我想采用路径“C:\Users\Jim\AppData\Local\Temp\”并删除“Temp\”部分。只留下“C:\ Users \ Jim \ AppData \ Local \”我已经用下面的代码解决了我的问题,但这假设“Temp”文件夹实际上称为“Temp”。有更好的办法吗?谢谢
if (Path.GetTempPath() != null) // Is it there?{
tempDir = Path.GetTempPath(); //Make a string out of it.
int iLastPos = tempDir.LastIndexOf(@"\");
if (Directory.Exists(tempDir) && iLastPos > tempDir.IndexOf(@"\"))
{
// Take the position of the last "/" and subtract 4.
// 4 is the lenghth of the word "temp".
tempDir = tempDir.Substring(0, iLastPos - 4);
}}
How can I use IndexOf with SubString to pick a specific Character when there are more than one of them? Here's my issue. I want to take the path "C:\Users\Jim\AppData\Local\Temp\" and remove the "Temp\" part. Leaving just "C:\Users\Jim\AppData\Local\" I have solved my problem with the code below but this assumes that the "Temp" folder is actually called "Temp". Is there a better way? Thanks
if (Path.GetTempPath() != null) // Is it there?{
tempDir = Path.GetTempPath(); //Make a string out of it.
int iLastPos = tempDir.LastIndexOf(@"\");
if (Directory.Exists(tempDir) && iLastPos > tempDir.IndexOf(@"\"))
{
// Take the position of the last "/" and subtract 4.
// 4 is the lenghth of the word "temp".
tempDir = tempDir.Substring(0, iLastPos - 4);
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更好的方法是使用
Directory.GetParent ()
或 < code>DirectoryInfo.Parent:(请注意,
Directory.GetParent(path)
只是为您提供 Temp 目录,因为它不知道该路径已经意味着是一个目录。)如果您确实想使用
LastIndexOf
,请使用 允许您指定起始位置的重载。The better way is to use
Directory.GetParent()
orDirectoryInfo.Parent
:(Note that
Directory.GetParent(path)
just gives you the Temp directory, as it doesn't understand that the path is already meant to be a directory.)If you really wanted to use
LastIndexOf
though, use the overload which allows you to specify the start location.为什么不直接使用系统类来处理这个问题呢?
Why not just handle this directly using the System classes?
其他回答者已经展示了实现您目标的最佳方法。为了进一步扩展您的知识,我建议您通常查看正则表达式来满足您的字符串匹配和替换需求。
在我意识到其他人已经解决了所有这些问题之前,我在自学编程生涯的头几年里做了可以想象到的最复杂的字符串操作,并且我拿起了 掌握正则表达式。我强烈推荐它。
删除最后一个目录的一种方法是使用以下正则表达式:
它可能看起来很神秘,但这实际上会从路径中删除最后一个项目,无论其名称如何,也无论是否有另一个
\ 最后。
The other answerers have shown the best way to accomplish your goal. In the interest of expanding your knowledge further, I suggest that you look at regular expressions for your string matching and replacement needs, in general.
I spent the first couple of years of my self-taught programming career doing the most convoluted string manipulation imaginable before I realized that someone else had already solved all these problems, and I picked up a copy of Mastering Regular Expressions. I strongly recommend it.
One way to strip off the last directory is with the following regular expression:
It may look cryptic, but this will actually remove the last item from the path, regardless of its name, and regardless of whether there is another
\
at the end.我会使用 DirectoryInfo 类。
I would use the DirectoryInfo class.