C#中的字符串大小问题 [英] String Size issue in c#

查看:97
本文介绍了C#中的字符串大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.当Filecontent大小约为时,我遇到异常:
filecontent.Length 4821980 int.

控件自动退出循环,并从头开始再次循环.我不明白这个问题.这有什么问题吗?

我这样做是为了为文本文件写大量或记录.请更正我或建议我替代此方法.
-------------------------------------------------- ------------------

I have the code as below . I am getting an exception when the Filecontent size is about:
filecontent.Length 4821980 int .

The control automatically exits from the loop and starts looping from first again. I don''t understand the issue. IS there any thing wrong in this.

I am doing this in order to write large number or records for the text file. Please correct me or suggest me an alternative for this.
--------------------------------------------------------------------

string filecontent = string.Empty;
                int t = 0;
                for (int i = 0; i < filingTable.Rows.Count; i++)
                {
                                  
                    if (i != 0)
                        filecontent += System.Environment.NewLine;

                    filecontent += fixedString(filingTable.Rows[i][0].ToString(), 20);
                    filecontent += fixedString(filingTable.Rows[i][1].ToString(), 1);
                    filecontent += fixedString(filingTable.Rows[i][2].ToString(), 8);
                    filecontent += fixedString(filingTable.Rows[i][3].ToString(), 11);
                    filecontent += fixedString(filingTable.Rows[i][4].ToString(), 9);

                    filecontent += fixedString(filingTable.Rows[i][5].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][6].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][7].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][8].ToString(), 40);
                    filecontent += fixedString(filingTable.Rows[i][9].ToString(), 40);

                    filecontent += fixedString(filingTable.Rows[i][10].ToString(), 180);

                    filecontent += fixedNumber(filingTable.Rows[i][11].ToString().TrimEnd(), 12);

                    filecontent += fixedNumber(filingTable.Rows[i][12].ToString(), 6);
                }

推荐答案

0)您应该使用StringBuilder对象来构建文本行.

0) You should be using the StringBuilder object to buuilt the line of text.

StringBuilder sb = new StringBuilder();
sb.Append(fixString(blah blah));
sb.Append(fixString(blah blah));
sb.Append(fixString(blah blah));
...



1)您一次应该写一行.

2)您仍然可能会遇到问题,因为文件不能为无限大小.顺便说一句,字符串最大只能为4gb.



1) You should be write a single line at a time.

2) You still might run into the problem because files cannot be an infinite size. BTW, a string can only be 4gb, max.


这篇关于C#中的字符串大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆