gpt4 book ai didi

c# - 使用 Linq 获取 List 集合中的子字符串

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:44 25 4
gpt4 key购买 nike

我有一个收藏 list 。

List<string> mycollections = new List<string>(new string[] 
{
"MyImages/Temp/bus.jpg",
"MyImages/Temp/car.jpg",
"MyImages/Temp/truck.jpg",
"MyImages/Temp/plane.jpg",
"MyImages/Temp/ship.jpg",
});

我只需要列表中的文件,例如bus.jpg、car.jpg.....。这里我不需要 "MyImages/Temp/" 同一列表中的字符串部分。

我尝试使用 Substring 和 Split with Linq 查询,但无法获得预期的结果。

最佳答案

使用Path.GetFileName而不是像这样的子字符串:

var fileNames = mycollections.Select(r => Path.GetFileName(r)).ToList();

对于输出:

var fileNames = mycollections.Select(r => Path.GetFileName(r));
foreach (var item in fileNames)
{
Console.WriteLine(item);
}

输出:

bus.jpg
car.jpg
truck.jpg
plane.jpg
ship.jpg

关于c# - 使用 Linq 获取 List<string> 集合中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368277/

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