String.Insert() problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
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
 
Back
Top