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

示例字符串

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

分割后的效果

www.cjavapy.com
questions
ask
user
end

join合成后的效果

www.cjavapy.com
www.cjavapy.com/questions
www.cjavapy.com/questions/ask
www.cjavapy.com/questions/ask/user
www.cjavapy.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)));