参考网址:https://blog.csdn.net/yenange/article/details/39637211

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Data;  
      
    namespace Study  
    {  
        public static class Program2  
        {  
            static void Main(string[] args)  
            {  
                string[] source = {  
                    null,  
                    string.Empty,  
                    "1234",  
                    "12345",  
                    "123456",  
                    "1234567890",  
                };  
                int charNum = 5;  
                int i = 1;  
                foreach (string str in source)  
                {  
                    Console.WriteLine("原字符串{0}:{1}", (i++).ToString(), str);  
                    string[] r = str.SplitByLen(charNum, "{0}/{1}){2}");  
                    foreach (string s in r)  
                    {  
                        Console.WriteLine("{0}", s);  
                    }  
                    Console.WriteLine();  
                }  
                Console.Read();  
            }  
      
            /// <returns></returns>  
            /// <summary>  
            /// 按字符串长度切分成数组  
            /// </summary>  
            /// <param name="str">原字符串</param>  
            /// <param name="separatorCharNum">切分长度</param>  
            /// <param name="prefixFormat">前缀格式</param>  
            /// <returns>字符串数组</returns>  
            public static string[] SplitByLen(this string str, int separatorCharNum, string prefixFormat)   
            {  
                string[] arr = SplitByLen(str, separatorCharNum);  
                if (arr.Length == 1)   
                {  
                    return arr;  
                }  
                List<string> list = new List<string>();  
                for(int i=1;i<=arr.Length;i++)   
                {  
                    list.Add(string.Format(prefixFormat,i.ToString(), arr.Length.ToString(), arr[i-1]));  
                }  
                return list.ToArray();  
            }  
      
            /// <summary>  
            /// 按字符串长度切分成数组  
            /// </summary>  
            /// <param name="str">原字符串</param>  
            /// <param name="separatorCharNum">切分长度</param>  
            /// <returns>字符串数组</returns>  
            public static string[] SplitByLen(this string str, int separatorCharNum)  
            {  
                if (string.IsNullOrEmpty(str) || str.Length <= separatorCharNum)   
                {  
                    return new string[] { str };  
                }  
                string tempStr = str;  
                List<string> strList = new List<string>();  
                int iMax = Convert.ToInt32(Math.Ceiling(str.Length / (separatorCharNum * 1.0)));//获取循环次数  
                for (int i = 1; i <= iMax; i++)  
                {  
                    string currMsg = tempStr.Substring(0, tempStr.Length > separatorCharNum ? separatorCharNum : tempStr.Length);  
                    strList.Add(currMsg);  
                    if (tempStr.Length > separatorCharNum)  
                    {  
                        tempStr = tempStr.Substring(separatorCharNum, tempStr.Length - separatorCharNum);  
                    }  
                }  
                return strList.ToArray();  
            }  
        }  
    }  
View Code

相关文章:

  • 2022-12-23
  • 2021-05-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
猜你喜欢
  • 2022-02-28
  • 2022-12-23
  • 2023-02-06
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
相关资源
相似解决方案