开发者

combine three parts

开发者 https://www.devze.com 2023-01-26 00:46 出处:网络
Path.combine can only combine two string, is there a qucik way to combine开发者_运维百科 three and four strings?Upgrade to .Net 4.0, which adds the overloads you\'re looking for.

Path.combine can only combine two string, is there a qucik way to combine开发者_运维百科 three and four strings?


Upgrade to .Net 4.0, which adds the overloads you're looking for.


If you're stuck in .Net 3.5, you can call Path.Combine multiple times.

If you have an IEnumerable<string>, you can write

string path = strings.Aggregate(Path.Combine);


I'm not a C# programmer, but something like

string s = Path.Combine("str1", Path.Combine("str2", Path.Combine("str3", "str4")));

Seems obvious.


If you can upgrade to .NET 4.0 it has what you are looking for.

Otherwise:

public string Combine(IEnumerable<string> strings) {
    return strings.Aggregate((x, y) => Path.Combine(x, y));
}

and

public string Combine(params string[] strings) {
    return Combine((IEnumerable<string>)strings);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消