zoukankan      html  css  js  c++  java
  • C# 如何使用长度来切分字符串

    参考网址: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

    自己参考的一个简单历程:

    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();  
            }  
        }
    }

  • 相关阅读:
    Oracle PL/SQL攻略
    Android数据库中查找一条数据 query方法详解
    验证视图MAC失败 Validation of ViewState MAC Failed
    长方体类
    用类的友元函数完成运算符的重载
    全国软件2. 三人年龄
    Android中实现带声音提示的Toast (自定义扩展Toast)
    js判断生效时间不得大于失效时间
    OpenGL运用辅助库创建规则几何对象
    Ubuntu13.04安装CUDA5.0
  • 原文地址:https://www.cnblogs.com/congcongdi/p/9214332.html
Copyright © 2011-2022 走看看