C#split函数原理

C#中split函数的实现原理

相信很多小伙伴跟我一样初入C#很想了解下Split函数其中的实现原理,具体实现程序如下

在这里插入图片描述输出结果如上图所示

具体实现代码如下。

using System;
using System.Collections;
namespace split
{
    class mySplit
    {
        static void Main(string[] args)
        {
            string str1 = "", str2 = "";
            if (args.Length != 2)
            {
                Console.WriteLine("请输入要分割的字符串:");
                str1 = Console.ReadLine();
                Console.WriteLine("请输入分割符:");
                str2 = Console.ReadLine();
                Console.WriteLine(" 分割出的数据如下: ");
            }
            else
            {
                str1 = args[0];
                str2 = args[1];
            }
            string[] output = null;
            output = split(str1, str2);
            for (int i = 0; i < output.Length; i++)
            {
                Console.Write("{0}"+" ", output[i]);
            }
            Console.WriteLine("被分割成{0}份字符串!", output.Length);
            DateTime dt = DateTime.Now;
            for (int i = 0; i < 1000; i++)
            {
                output = split(str1, str2);
                output = null;
            }
            System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("&{2}");
            dt = DateTime.Now;
            for (int j = 0; j < 1000; j++)
            {
                output = rg.Split(str1);
                output = null;
            }
            Console.ReadKey();
        } 
        //分割函数开始
        public static string[] split(string strinput, string sp)
        {
            string tmp = "";
            int strlen = 0, splen = 0;
            int found = 0;
            string[] rt = null;
            try
            {
                if (strinput == null || sp == null || strinput.Length == 0 || sp.Length == 0)
                {
                    return null;  //初始化一个数组列表(当做动态数组)
                }                  
                ArrayList tmp3 = new ArrayList();
                strlen = strinput.Length;
                splen = sp.Length;
                for (int i = 0; i < strlen; i++)
                {  //查找分隔符
                    found = strinput.IndexOf(sp, i);
                    if (found >= 0)
                    {
                        tmp = "";  //取分隔符前的字符串
                        tmp = strinput.Substring(i, found - i);  //添加到数组列表
                        tmp3.Add(tmp);
                        i = found + splen - 1;
                    }
                    else
                    {
                        string tmp2 = "";  //取最后的字符串
                        tmp2 = strinput.Substring(i);
                        if (tmp2 != "")
                            tmp3.Add(tmp2);
                        break;
                    }
                }  //将动态数组的维数设置成实际存在的元素个数,因为数组列表是以16的倍数递增维数的
                tmp3.TrimToSize();  //转换数组列表为字符串数组,并返回。
                rt = (string[])tmp3.ToArray(typeof(String));
                tmp3.Clear();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
                throw e;
            }
            return rt;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日落、看夕阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值