从文件路径中删除“head”目录

时间:2013-02-12 18:54:25

标签: c# .net vb.net string

在.NET(VB或C#)中有没有人知道从文件路径String中删除'head'目录的简单方法,这意味着如果我的路径看起来像这样:Directory1/Directory2/Directory3我想得到Directory2/Directory3回来了。我知道有一些方法可以做到这一点,比如将它拆分成一个数组,然后从第二个元素开始将它连接起来,我只是觉得这样,这是一种非常低效的方法,并且想知道是否有这是一个更好的方法。

4 个答案:

答案 0 :(得分:4)

这取决于你在寻找什么。如果您知道事情的格式为dir1/dir2/dir3/dir4...,那么您可以查找第一个/并在此之后采取一切措施:

string dir = "dir1/dir2/dir3";
var pos = dir.IndexOf('/');
if (pos != -1)
{
    result = dir.Substring(pos+1);
}

如果您还可以接受表单c:\dir\dir\file.ext\\server\dir\dir\file.ext的完整路径名,那么您可能希望确保首先将任何相对路径设置为完整路径。然后使用System.IO.Path类中的方法在使用上述IndexOf技巧之前提取驱动器或服务器名称。

答案 1 :(得分:0)

查看System.IO.Path课程。对于这种事情,它有许多有用的方法。

例如,您可以使用它的GetPathRoot()方法作为对String.Replace()的调用的一部分,如下所示:

public string RemoveHead(string path)
{
    return path.Replace(Path.GetPathRoot(path), "");
}

我没有Visual Studio方便,所以可能需要一些调整来解释分隔符或驱动器号,但它应该给你这个想法。

答案 2 :(得分:0)

如果您想要正确和高效,

string path=@"dir1/dir2/dir3";
path=path.Substring(path.IndexOf(System.IO.Path.DirectorySeparatorChar)+1);

只创建了一个新字符串。

答案 3 :(得分:0)

        /// <summary>
    /// Removes head.
    /// </summary>
    /// <param name="path">The path to drop head.</param>
    /// <param name="retainSeparator">If to retain separator before next folder when deleting head.</param>
    /// <returns>New path.</returns>
    public static string GetPathWithoutHead (string path, bool retainSeparator = false)
    {
        if (path == null) 
        {
            return path;
        }
        if (string.IsNullOrWhiteSpace (path)) 
        {
             throw new ArgumentException(path, "The path is not of a legal form.");
        }

        var root = System.IO.Path.GetPathRoot (path);
        if (!string.IsNullOrEmpty(root) && !StartsWithAny(root,System.IO.Path.DirectorySeparatorChar,System.IO.Path.AltDirectorySeparatorChar))
        {               
            return path.Remove(0,root.Length);
        }

        var sep = path.IndexOf(System.IO.Path.DirectorySeparatorChar);
        var altSep = path.IndexOf(System.IO.Path.AltDirectorySeparatorChar);
        var pos = MaxPositiveOrMinusOne (sep, altSep);
        if (pos == -1)
        {
            return string.Empty;
        }

        if (pos == 0) 
        {
            return GetPathWithoutHead(path.Substring(pos+1), retainSeparator);
        }

        var eatSeparator = !retainSeparator ? 1 : 0;
        return path.Substring(pos+eatSeparator);
    }

    /// <summary>
    /// Startses the with.
    /// </summary>
    /// <returns><c>true</c>, if with was startsed, <c>false</c> otherwise.</returns>
    /// <param name="val">Value.</param>
    /// <param name="maxLength">Max length.</param>
    /// <param name="chars">Chars.</param>
    private static bool StartsWithAny(string value, params char[] chars)
    {
        foreach (var c in chars) 
        {
            if (value[0] == c) 
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// Maxs the positive or minus one.
    /// </summary>
    /// <returns>The positive or minus one.</returns>
    /// <param name="val1">Val1.</param>
    /// <param name="val2">Val2.</param>
    private static int MaxPositiveOrMinusOne(int val1, int val2)
    {
        if (val1 < 0 && val2 < 0)
        {
            return -1;
        }
        return Math.Max(val1,val2: val2);
    }

Several unit tests are here

相关问题