开发手册 欢迎您!
软件开发者资料库

.NET(C#) 分割(split)url字符串在合成(join)多个子路径url

本文主要介绍.NET(C#)中,url字符串通过“/”进行分割成多字符串,然后在join合成多个子路径url字符串。

示例字符串

www.wonhero.com/questions/ask/user/end

分割后的效果

www.wonhero.com
questions
ask
user
end

join合成后的效果

www.wonhero.comwww.wonhero.com/questionswww.wonhero.com/questions/askwww.wonhero.com/questions/ask/userwww.wonhero.com/questions/ask/user/end

1、通过foreach实现

var splitted = "www.stackoverflow.com/questions/ask/user/end".Split('/').ToList();string full = "";foreach (var part in splitted){    full=$"{full}/{part}"    Console.Write(full);}

2、使用Linq实现

var splitted = "www.stackoverflow.com/questions/ask/user/end".Split('/').ToList();
var list = splitted.Select((x, i) => string.Join("/", a.Take(i + 1)));