gpt4 book ai didi

c# - 按基本路径过滤路径列表的最有效/优雅的方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:30:10 24 4
gpt4 key购买 nike

按基本路径过滤所有路径的最有效/最优雅的方法是什么?

我有一个路径列表和一个基本路径,我想要一个获取基本路径的子路径列表:

public IList<string> FilterPathList(IList<string> paths, string basePath)
{
// return a list of paths that are children of the base path
}

示例输入:

c:\foo\bar\file1
c:\foo\bar\file2
c:\foo\bar\dir1\file11
c:\foo\bar\dir2\file
c:\foo\other\file1

Base path -> c:\foo\bar

预期输出:

c:\foo\bar\file1
c:\foo\bar\file2
c:\foo\bar\dir1\file11
c:\foo\bar\dir2\file

最佳答案

类似于:

paths.Where(p => p.StartsWith(basePath)).ToList();

您可能想要充实 Where 以使比较不区分大小写,当然,除非您将大小写标准化。

如果它在列表中,这也将返回基本路径。

关于c# - 按基本路径过滤路径列表的最有效/优雅的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com