ขั้นแรก ตั้งค่าสตริง เช่น เส้นทางไดเรกทอรี Windows ของคุณ -
string str = @"D:\Downloads\Amit";
ตอนนี้ใช้เมธอด Split() และแยกทุกที่ที่ \ เกิดขึ้น -
str.Split(' \\') ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
class Program {
static void Main() {
string str = @"D:\Downloads\Amit";
Console.WriteLine("Directory...\n"+str);
string[] myStr = str.Split('\\');
Console.WriteLine("\nSplit...");
foreach (string ch in myStr) {
Console.WriteLine(ch);
}
}
} ผลลัพธ์
Directory... D:\Downloads\Amit Split... D: Downloads Amit