示例字符串
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)));