Wednesday, March 28, 2012

String.Insert() problem

dear :

i have the following problem:

string sAttachmentBody;
int nCount = 0;
int nMaxLineLength = 77;

//sAttachmentBody.Length may reaches (560960 chars)

for(int i=0 ; i < sAttachmentBody.Length ; i++)
{
if(nCount % nMaxLineLength == 0 )
{
try
{
sAttachmentBody = sAttachmentBody.Insert(i,"\n\r");
}
catch(Exception ex)
{
wr = new System.IO.StreamWriter("C:\\stringerror.txt");
wr.WriteLine("Message = " + ex.Message );
wr.WriteLine("Stack = " + ex.StackTrace );
wr.Close();
}

nCount = 0;
}
}

the exception
Stack = at System.String.Insert(Int32 startIndex, String value)
at Utility.Email.SetAttachmentBody(String sAttachmentBody)

the exception occured ONLY if the length is to big , is there a replacement
for insert function because as I wrote the length may reaches millionsStringBuilder class might be what you are looking for.

"Raed Sawalha" wrote:

> dear :
> i have the following problem:
>
> string sAttachmentBody;
> int nCount = 0;
> int nMaxLineLength = 77;
> //sAttachmentBody.Length may reaches (560960 chars)
> for(int i=0 ; i < sAttachmentBody.Length ; i++)
> {
> if(nCount % nMaxLineLength == 0 )
> {
> try
> {
> sAttachmentBody = sAttachmentBody.Insert(i,"\n\r");
> }
> catch(Exception ex)
> {
> wr = new System.IO.StreamWriter("C:\\stringerror.txt");
> wr.WriteLine("Message = " + ex.Message );
> wr.WriteLine("Stack = " + ex.StackTrace );
> wr.Close();
> }
> nCount = 0;
> }
> }
> the exception
> Stack = at System.String.Insert(Int32 startIndex, String value)
> at Utility.Email.SetAttachmentBody(String sAttachmentBody)
> the exception occured ONLY if the length is to big , is there a replacement
> for insert function because as I wrote the length may reaches millions
my problem is the index where I want to place the char all the overloaded
function is int
what should I do

"Tu-Thach" wrote:

> StringBuilder class might be what you are looking for.
> "Raed Sawalha" wrote:
> > dear :
> > i have the following problem:
> > string sAttachmentBody;
> > int nCount = 0;
> > int nMaxLineLength = 77;
> > //sAttachmentBody.Length may reaches (560960 chars)
> > for(int i=0 ; i < sAttachmentBody.Length ; i++)
> > {
> > if(nCount % nMaxLineLength == 0 )
> > {
> > try
> > {
> > sAttachmentBody = sAttachmentBody.Insert(i,"\n\r");
> > }
> > catch(Exception ex)
> > {
> > wr = new System.IO.StreamWriter("C:\\stringerror.txt");
> > wr.WriteLine("Message = " + ex.Message );
> > wr.WriteLine("Stack = " + ex.StackTrace );
> > wr.Close();
> > }
> > nCount = 0;
> > }
> > }
> > the exception
> > Stack = at System.String.Insert(Int32 startIndex, String value)
> > at Utility.Email.SetAttachmentBody(String sAttachmentBody)
> > the exception occured ONLY if the length is to big , is there a replacement
> > for insert function because as I wrote the length may reaches millions
int is sufficient for for handling millions of characters. I recommend that
you use the StringBuilder class and build up your string with "\r\n" instead
of replacing the string like you are doing now.

"Raed Sawalha" wrote:

> my problem is the index where I want to place the char all the overloaded
> function is int
> what should I do
> "Tu-Thach" wrote:
> > StringBuilder class might be what you are looking for.
> > "Raed Sawalha" wrote:
> > > dear :
> > > > i have the following problem:
> > > > > string sAttachmentBody;
> > > int nCount = 0;
> > > int nMaxLineLength = 77;
> > > > //sAttachmentBody.Length may reaches (560960 chars)
> > > > for(int i=0 ; i < sAttachmentBody.Length ; i++)
> > > {
> > > if(nCount % nMaxLineLength == 0 )
> > > {
> > > try
> > > {
> > > sAttachmentBody = sAttachmentBody.Insert(i,"\n\r");
> > > }
> > > catch(Exception ex)
> > > {
> > > wr = new System.IO.StreamWriter("C:\\stringerror.txt");
> > > wr.WriteLine("Message = " + ex.Message );
> > > wr.WriteLine("Stack = " + ex.StackTrace );
> > > wr.Close();
> > > }
> > > > nCount = 0;
> > > }
> > > }
> > > > the exception
> > > Stack = at System.String.Insert(Int32 startIndex, String value)
> > > at Utility.Email.SetAttachmentBody(String sAttachmentBody)
> > > > the exception occured ONLY if the length is to big , is there a replacement
> > > for insert function because as I wrote the length may reaches millions
=?Utf-8?B?UmFlZCBTYXdhbGhh?= <RaedSawalha@.discussions.microsoft.com>
confessed in news:FA6628D8-8568-4E71-A1B9-337E43A04EC0@.microsoft.com:

> dear :
> i have the following problem:
>
> string sAttachmentBody;
> int nCount = 0;
> int nMaxLineLength = 77;
> //sAttachmentBody.Length may reaches (560960 chars)
> for(int i=0 ; i < sAttachmentBody.Length ; i++)
> {
> if(nCount % nMaxLineLength == 0 )
> {
> try
> {
> sAttachmentBody = sAttachmentBody.Insert(i,"\n\r");
> }
> catch(Exception ex)
> {
> wr = new System.IO.StreamWriter("C:\\stringerror.txt");
> wr.WriteLine("Message = " + ex.Message );
> wr.WriteLine("Stack = " + ex.StackTrace );
> wr.Close();
> }
> nCount = 0;
> }
> }
> the exception
> Stack = at System.String.Insert(Int32 startIndex, String value)
> at Utility.Email.SetAttachmentBody(String sAttachmentBody)
> the exception occured ONLY if the length is to big , is there a
replacement
> for insert function because as I wrote the length may reaches millions

Oh man, that's a killer memory operation.

Use a StringBuilder, not a String!

-- ipgrunt
use something larger. Perhaps an int64?

0 comments:

Post a Comment