参考网址: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(); } } }
自己参考的一个简单历程:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ChaiF { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string[] arr=SplitByLen(textBox1.Text,4); textBox2.Text=arr[0].ToString(); textBox3.Text=arr[1].ToString(); } public string[] SplitByLen(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(); } } }