單純 `string.Split(',')` 無法處理資料本身包含逗號的情況 (如 `"reser,vation,rental,type"`)。需支援跳脫字元 (如 `\,` 代表資料內逗號) 的分割邏輯。
使用正向頻寬零寬度斷言 `(?
string input = "reser\\,vation,rental,type";
string[] parts = Regex.Split(input, @"(?
public static string[] SplitWithEscape(string input, char separator = ',', char escape = '\\')
{
if (string.IsNullOrEmpty(input)) return new string[0];
string pattern = $@"(?
| 輸入 | 輸出 |
|---|---|
| `a,b,c` | `["a","b","c"]` |
| `a\,b,c` | `["a,b","c"]` |
| `a\\,b,c` | `["a\\","b","c"]` (雙反斜線 = 字面反斜線 + 分隔符) |
| `\\,` | `["\\", ""]` |
依專案依賴策略選擇:無額外套件 → Regex;標準 CSV → TextFieldParser/CsvHelper;極致效能 → 手寫解析器。