[ASP .NET]以多字元分割文字
平常在分割字串時,最常用的語法就是Split
string[] t = "test,test23,test1".Split(',');
//t = ["test", "test23", "test1"];
可是這樣寫只能單字元分割字串,如果要以多字元分割字串的話要改用
string[] t = "test,,test23,,,,test1".Split(new string[] { ",," }, System.StringSplitOptions.RemoveEmptyEntries);
//t = ["test", "test23", "test1"];
也可以用多個分割文字/字元來分割
string[] t = "test,:test23,,\ttest1".Split(new char[] { ' ', ',', '.', ':', '\t' }, StringSplitOptions.RemoveEmptyEntries);
//t = ["test", "test23", "test1"];
string[] t2 = "test,:test23,,\ttest1".Split(new string[] { ",:", ",\t" }, StringSplitOptions.RemoveEmptyEntries);
//t2 = ["test", "test23,", "test1"];
參考資料
https://docs.microsoft.com/zh-tw/dotnet/csharp/how-to/parse-strings-using-split