首页 > 其他 > 详细

字符串和Stream 分割

时间:2019-08-07 14:24:26      阅读:97      评论:0      收藏:0      [点我收藏+]

字符串分割:

public List<StringBuilder> SplitLength(StringBuilder SourceString, int Length)
        {
            List<StringBuilder> list = new List<StringBuilder>();
            try
            {                
                Length = Length * 1024 * 1024 * 2;


                for (int i = 0; i < SourceString.Length; i += Length)
                {
                    if ((SourceString.Length - i) >= Length)
                        list.Add(new StringBuilder(SourceString.ToString(i, Length)));
                    else
                        list.Add(new StringBuilder(SourceString.ToString(i, SourceString.Length - i)));
                }                
            }
            catch (Exception e)
            { }
            return list;
        }

Stream 分割:

private static List<byte[]> SplitLength(MemoryStream stream)
        {
            int Length = 2 * 1024 * 1024;
            List<byte[]> bytelist = new List<byte[]>();
            int streamlength = (int)stream.Length;
            stream.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < streamlength; i += Length)
            {
                if ((streamlength - i) >= Length)
                {
                    byte[] bt = new byte[Length];
                    stream.Read(bt, 0, Length);
                    bytelist.Add(bt);
                }
                else
                {
                    int lastlen = streamlength - i;
                    byte[] bt = new byte[lastlen];
                    stream.Read(bt, 0, lastlen);
                    bytelist.Add(bt);
                }
            }
            return bytelist;
        }

 

字符串和Stream 分割

原文:https://www.cnblogs.com/lili9696189/p/11314578.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!