开发者

C# Path operations

开发者 https://www.devze.com 2023-01-27 00:41 出处:网络
I need to get \"first_level\" and \"second_level\\third_level\" from the original path \"first_level\\second_level\\third_level\", something that splits the path into two part by the first separator.

I need to get "first_level" and "second_level\third_level" from the original path "first_level\second_level\third_level", something that splits the path into two part by the first separator. 开发者_JS百科Is there any C# method in .net library that does that?


Use the Split overload that takes a count for the maximum number of substrings to return:

string input = @"first_level\second_level\third_level";
string[] result = input.Split(new[] { '\\' }, 2);
foreach (string s in result)
    Console.WriteLine(s);

// result[0] = "first_level"
// result[1] = "second_level\third_level"


string myPath = @"first_level\second_level\third_level";

string[] levels = myPath.Split('\\');

and

    level[0] will be equal to first_level
    level[2] will be equal to second_level
    level[3] will be equal to third_level

you asking this?

0

精彩评论

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