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

  • 相关阅读:
    [C++][编程风格]C++命名规则
    [Android]搜索关键字飞入飞出效果 (转)
    android,性能优化,内存优化管理,高级缓存 (转)
    Java内存泄露原因详解
    Java之线程(2) 调时器
    Java之线程(1) 传统线程机制的回顾
    Hibernate(1) 阻抗不匹配
    No4.传统线程同步通信技术
    Android内存溢出
    MAT Memory Analyzer Tool 插件装配(图解)(转)
  • 原文地址:https://www.cnblogs.com/congcongdi/p/9214332.html
Copyright © 2011-2022 走看看