2015年9月17日 星期四

[C#] Split 分割字串

用法一 :最普通的字串分割

split() 括號記得用單引號包起來


            string tmp = "AA#BB#CC";
            string[] strSplit = tmp.Split('#');
            string GetValue = strSplit[0]; // 拿到 AA
            GetValue = tmp.Split('#')[1]; //直接拿到字串 BB
            //多字元分割
            string tmp2 = "AA#|BB$$CC#|";
            string[] strSplit2 = tmp2.Split(new string[] { "#|","$$" }, StringSplitOptions.RemoveEmptyEntries);
            string GetValue2 = "";
            foreach(string s in strSplit2)
            {
                GetValue2 += s + " "; //拿到 AA BB CC
            }

             //20160311 新增 分割換行的寫法
             string[] NewLine = tmp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
             
             //textarea 會把換行符號轉成\n 所以也可以這樣寫
             string[] NewLine2 = tmp.products.Replace("\n","|").Split('|');