Пример #1
0
        /// <summary>
        /// 用一个字符串将一个原始字符串分割成字符串数组,而.net中只能使用char进行分割。
        /// </summary>
        /// <param name="sourceString">输入的字符串</param>
        /// <param name="p">字符串型的分割符</param>
        /// <param name="splitoption">指定包含还是省略返回值中的空子字符串。</param>
        /// <returns>希望的数组</returns>
        public static string[] Split(string sourceString, string p, StringSplitOptions splitoption)
        {
            string        substr;
            int           found   = 0;
            List <string> rtnList = new List <string>();

            try
            {
                if (string.IsNullOrEmpty(sourceString) || string.IsNullOrEmpty(p))
                {
                    return(null);
                }
                for (int i = 0; i < sourceString.Length; i++)
                {
                    //查找分隔符
                    found = sourceString.IndexOf(p, i);
                    if (found >= 0)
                    {
                        //取分隔符前的字符串
                        substr = sourceString.Substring(i, found - i);
                        switch (splitoption)
                        {
                        case StringSplitOptions.None:
                            rtnList.Add(substr);
                            break;

                        case StringSplitOptions.RemoveEmptyEntries:
                            if (!string.IsNullOrEmpty(substr))
                            {
                                rtnList.Add(substr);
                            }
                            break;

                        default:
                            Debug.Fail(splitoption.ToString());
                            break;
                        }
                        i = found + p.Length - 1;
                    }
                    else
                    {
                        rtnList.Add(sourceString.Substring(i));
                        break;
                    }
                }
            }
            catch
            {
                throw;
            }
            return(rtnList.ToArray());
        }
Пример #2
0
 /// <summary>
 /// 用一个字符串将一个原始字符串分割成字符串数组,而.net中只能使用char进行分割。
 /// </summary>
 /// <param name="sourceString">输入的字符串</param>
 /// <param name="p">字符串型的分割符</param>
 /// <param name="splitoption">指定包含还是省略返回值中的空子字符串。</param>
 /// <returns>希望的数组</returns>
 public static string[] Split(string sourceString, string p, StringSplitOptions splitoption)
 {
     string substr;
     int found = 0;
     List<string> rtnList = new List<string>();
     try
     {
         if (string.IsNullOrEmpty(sourceString) || string.IsNullOrEmpty(p))
         {
             return null;
         }
         for (int i = 0; i < sourceString.Length; i++)
         {
             //查找分隔符
             found = sourceString.IndexOf(p, i);
             if (found >= 0)
             {
                 //取分隔符前的字符串
                 substr = sourceString.Substring(i, found - i);
                 switch (splitoption)
                 {
                     case StringSplitOptions.None:
                         rtnList.Add(substr);
                         break;
                     case StringSplitOptions.RemoveEmptyEntries:
                         if (!string.IsNullOrEmpty(substr))
                         {
                             rtnList.Add(substr);
                         }
                         break;
                     default:
                         Debug.Fail(splitoption.ToString());
                         break;
                 }
                 i = found + p.Length - 1;
             }
             else
             {
                 rtnList.Add(sourceString.Substring(i));
                 break;
             }
         }
     }
     catch
     {
         throw;
     }
     return rtnList.ToArray();
 }