首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#字符串瓜分

2012-10-27 
C#字符串分割如图 我想把图中textbox 的字符串 每6KB写入一个txt 分别命名 1.txt 2.txt 3.txt .....请问怎

C#字符串分割


如图 我想把图中textbox 的字符串 每6KB写入一个txt 分别命名 1.txt 2.txt 3.txt .....
请问怎么样分割? txt总个数不超过9 个

麻烦兄弟们贴下代码 效率比较高的

[解决办法]

C# code
private void Form1_Load(object sender, EventArgs e)        {            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();            const int SIZE = 10;        //方便测试这里设置小一点            var input = "AWfs测试_fs2测试AS测试34再测试一下;&)$@Lrwer{DWUEND.";            var buffer = new byte[SIZE];            var chars = Encoding.UTF8.GetChars(buffer);     //其实是Size的一半,C#中一个字符占两字节            //AsyncCallback callBack = args =>            //{            //    //弄了个listBox反馈结果             //    if (this.listBox1.InvokeRequired)            //        this.listBox1.Invoke(new Action(() => this.listBox1.Items.Add(string.Format("\r\nHad written file: {0}.txt", (int)args.AsyncState))));            //};            sw.Start();            using (var sr = new System.IO.StringReader(input))            {                int offset = 0, times = 0;                IAsyncResult asyResult = null;                while (times < 10 && sr.Peek() > -1)                {                    //读到的字符数量,上面忽略了它,在最后一个文件会有bug                    offset = sr.Read(chars, 0, chars.Length);                    buffer = Encoding.UTF8.GetBytes(chars, 0, offset);                    using (var fs = new FileStream(Application.StartupPath + "\\" + ++times + ".txt", FileMode.Create))                    {                        //异步写数据,无回调                        //asyResult = fs.BeginWrite(buffer, 0, offset, callBack, times);                        asyResult = fs.BeginWrite(buffer, 0, buffer.Length, null, null);                        fs.EndWrite(asyResult);                        fs.Flush();                        fs.Close();                    }                 }            }            sw.Stop();            this.label1.Text = sw.ElapsedTicks.ToString();             //上面那个用了回调是2001392 左右,去掉后38563,提升很多             } 

热点排行