字符串和Stream 分割

字符串分割:

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;
        }
原文地址:https://www.cnblogs.com/lili9696189/p/11314578.html