PDA

View Full Version : Looping through string varible


Cipher
10-04-2006, 04:32 PM
Hi all..
I'm making a custom HTMLarea for myself, i'm using special tags i made to modify text formating, like the size and color,......., Just like this forums HTMLarea
Here's what i reached at last

private string ModifyBody(string Body)
{
char[] charBody = Body.ToCharArray();
StringBuilder sb = new StringBuilder();
string strBody = "", modBody = "";
string rtg, ltg;

for (int i = 0; i < charBody.Length; i++)
{
sb.Append(charBody[i].ToString());
strBody = sb.ToString();

//modify size
if (strBody.IndexOf("") != -1)
{
rtg = "[size=";
ltg = "";
int start = strBody.IndexOf(rtg);//0;
int end = (strBody.IndexOf(ltg) + ltg.Length);//(i + 1);

// cut the tags and text in tags and put them in other string
modBody = strBody.Substring(start, end);

// modify the tags and replace them with html tags
modBody = modBody.Replace("", ">");

// place the new modified string in the sb variable
sb.Replace(strBody.Substring(start, end), modBody);

strBody = sb.ToString();
//break;
}
}

return strBody;
}

and here's the variable value that i'm trying to modify

[size= 7]just new function
all new body function
Color Text
the one body function

not that i intended to leave this blank space so this forum wont parse it
and here's the error i get when i execute the code

Index and length must refer to a location within the string.
Parameter name: length
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length

Source Error:

Line 70:
Line 71: // cut the tags and text in tags and put them in other string
Line 72: modBody = strBody.Substring(start, end);
Line 73:
Line 74: // modify the tags and replace them with html tags

when i try to use break keyword, it works pretty fine with the first line.
also check out this image that i took while trying to debug it, it will show that the string variable is correct and going fine.
Thank you for your help.

http://www11.brinkster.com/cipherk/stringLoop.JPG