在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
有时我们需将一个字符串用另一个字符串来分割成字符串字组。而C#中string.split只提供用char来分割。怎么办? 用的时候直接调用第一个函数 /// <summary> /// 将字符串分割成数组 /// </summary> /// <param name="strSource"></param> /// <param name="strSplit"></param> /// <returns></returns> public string[] StringSplit(string strSource,string strSplit) { string[] strtmp = new string[1]; int index = strSource.IndexOf(strSplit,0); if(index<0) { strtmp[0] = strSource; return strtmp; } else { strtmp[0] = strSource.Substring(0,index); return StringSplit(strSource.Substring(index+strSplit.Length),strSplit,strtmp); } } /// <summary> /// 采用递归将字符串分割成数组 /// </summary> /// <param name="strSource"></param> /// <param name="strSplit"></param> /// <param name="attachArray"></param> /// <returns></returns> private string[] StringSplit(string strSource,string strSplit,string[] attachArray) { string[] strtmp = new string[attachArray.Length+1]; attachArray.CopyTo(strtmp,0); int index = strSource.IndexOf(strSplit,0); if(index<0) { strtmp[attachArray.Length] = strSource; return strtmp; } else { strtmp[attachArray.Length] = strSource.Substring(0,index); return StringSplit(strSource.Substring(index+strSplit.Length),strSplit,strtmp); } } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论