当有多个字符时,如何使用 IndexOf 来选择特定字符?

发布于 2024-08-28 13:34:36 字数 608 浏览 14 评论 0原文

当有多个字符时,如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

身边 2024-09-04 13:34:36

更好的方法是使用 Directory.GetParent ()< code>DirectoryInfo.Parent:(

using System;
using System.IO;

class Test
{
    static void Main()
    {
        string path = @"C:\Users\Jim\AppData\Local\Temp\";
        DirectoryInfo dir = new DirectoryInfo(path);
        DirectoryInfo parent = dir.Parent;
        Console.WriteLine(parent.FullName);
    }    
}

请注意,Directory.GetParent(path) 只是为您提供 Temp 目录,因为它不知道该路径已经意味着是一个目录。)

如果您确实想使用 LastIndexOf,请使用 允许您指定起始位置的重载

The better way is to use Directory.GetParent() or DirectoryInfo.Parent:

using System;
using System.IO;

class Test
{
    static void Main()
    {
        string path = @"C:\Users\Jim\AppData\Local\Temp\";
        DirectoryInfo dir = new DirectoryInfo(path);
        DirectoryInfo parent = dir.Parent;
        Console.WriteLine(parent.FullName);
    }    
}

(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.

不回头走下去 2024-09-04 13:34:36

为什么不直接使用系统类来处理这个问题呢?

string folder = Environment.GetFolder(Environment.SpecialFolder.LocalApplicationData);

Why not just handle this directly using the System classes?

string folder = Environment.GetFolder(Environment.SpecialFolder.LocalApplicationData);
2024-09-04 13:34:36

其他回答者已经展示了实现您目标的最佳方法。为了进一步扩展您的知识,我建议您通常查看正则表达式来满足您的字符串匹配和替换需求。

在我意识到其他人已经解决了所有这些问题之前,我在自学编程生涯的头几年里做了可以想象到的最复杂的字符串操作,并且我拿起了 掌握正则表达式。我强烈推荐它。

删除最后一个目录的一种方法是使用以下正则表达式:

tempDir = Regex.Match(tempDir, @".*(?=\\[^\\]+)\\?").Value;

它可能看起来很神秘,但这实际上会从路径中删除最后一个项目,无论其名称如何,也无论是否有另一个 \ 最后。

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:

tempDir = Regex.Match(tempDir, @".*(?=\\[^\\]+)\\?").Value;

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.

亚希 2024-09-04 13:34:36

我会使用 DirectoryInfo 类。

DirectoryInfo tempDirectory = new DirectoryInfo(Path.GetTempPath());            
DirectoryInfo tempDirectoryParent = tempDirectory.Parent;

I would use the DirectoryInfo class.

DirectoryInfo tempDirectory = new DirectoryInfo(Path.GetTempPath());            
DirectoryInfo tempDirectoryParent = tempDirectory.Parent;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文