简体   繁体   中英

C# Path operations

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. Is there any C# method in .net library that does that?

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?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM