故如虹,知恩;故如月,知明
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

c#自己实现拆分字符串的方法

2649人阅读 2018/10/12 10:11 总访问:3841943 评论:0 收藏:0 手机
分类: .NET

使用字符串分割方法即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Program3
    {
        public static string[] Split(string source, string separtor)
        {
            List<string> strList = new List<string>();
            while (true)
            {
                if (string.IsNullOrWhiteSpace(source))
                    break;
                //找到需要拆分字符串的位置
                int poi = source.IndexOf(separtor);
                if (poi < 0)
                {
                    strList.Add(source);
                    break;
                }
                string item = source.Substring(0, poi);
                if (!string.IsNullOrWhiteSpace(item))
                {
                    strList.Add(item);
                }
                //去已经拆分的和分割需要的字符串去掉,留下剩下的继续拆
                source = source.Substring(poi + separtor.Length);
            }
            return strList.ToArray();
        }

        static void Main(string[] args)
        {
            string str = "aaa|||bbb|||||ccc||||||ddd|||";
            //3,0-3  aaa -->bbb|||ccc
            //bbb  ccc
            string[] strs = Split(str, "|||");
        }
    }
}


欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价