3

我有一个链接,子类别重复了很多次。如果它们在某个列表中,也只想保持重复。但也要保留链接的最后一部分 About Video Example1 Example2

www.example.com/About/About/Videos/Videos/Videos/Featured/5-great-videos

应该

www.example.com/about/videos/5-great-videos

有什么帮助吗?

4

2 回答 2

2

这个怎么样,使用 LINQ

string str = "www.example.com/About/About/Videos/Videos/Videos/Featured/5-great-videos";
var result = str.Split('/').GroupBy(x=>x).Select(x=>x.Key).Aggregate((a,b)=>a+"/"+b);
于 2013-06-25T00:39:12.607 回答
-1

如果您不想使用 linq:

string url = "www.example.com/About/About/Videos/Videos/Videos/Featured/5-great-videos";
List<string> urlList = new List<string>();
foreach (string urlToken in url.Split('/'))
    if (!urlList.Contains(urlToken)) urlList.Add(urlToken);
url = String.Join("/", urlList.ToArray());
于 2013-06-25T01:19:14.537 回答