Friday, March 16, 2012

Looping through string variable!

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
some text here
then i replace this tags with html tags"<font size=1>some text here</font>" using the Replace method for string, and it works pretty fine, but the problem is that it changes some text that i dont want change, like if i posted a code containge "[]" for looping for example.
So what i want is to loop through the text variable and change each tag i want to change. How can i do that?!

Thank you very much.

Hi,

I did not get your question, you need to explain what you want to do in detail. If you want to replace your tags like "[ ]" with HTML tags like <font>, then obviously the string will refer to this new instance. Do you want to preserve the original text (containing your special tags)?

Give some more examples on what you want to achieve and problems you are facing.

Vivek

i'm so sorry, the tags i typed that i wanted to change wasnt written bec of the editor of this forum

http://www11.brinkster.com/cipherk/string.txt

try out this link it shows what i mean


Maybe you can use regular expression to do this.

But I can't understand the string.txt completely, so can't give available suggestion.


Why dont you parse the string character by character and append the results in a StringBuilder object? To get a particular character you can use

myString.Substring(0,1) //the 0 here can be a variable if you pass through a loop from 0 to myString.Length

Hope this helps,

Vivek


How exactly could i parse the string by character?! Here's what i tried and none worked fine.

string[] arrBody = Body.Split();for (int i = 0; i < arrBody.Length; i++) {//newBody += arrBody[i].ToString() + "<BR>";if (arrBody[i].IndexOf(") != -1) { arrBody[i"> = arrBody[i].Replace("[size=","<font size="); arrBody[i] = arrBody[i].Replace("]",">"); }if (arrBody[i].IndexOf("") != -1) { arrBody[i] = arrBody[i].Replace("[/size]","</font>"); } } Body =string.Concat(arrBody);
and ofcourse when i conacted the array it returns all the body in one line
and when i tried the following code, it returned each character which i couldnt use to find the strings i want to change
 
char[] charBody = Body.ToCharArray();string newBody ="";for (int i = 0; i < Body.Length; i++) { newBody += charBody[i].ToString() +"<br />"; } Body = newBody;

i tried also StringBuilder and all didnt workSmile

in the second code, i used this line

newBody += charBody[i].ToString() +"<br />";

just to make sure how string.ToCharArray works which i dnt think would be helpfull 4 me, but i'm not sure ofcourse


would you check out this and tell me why this error occurs

private string ModifyBody(string Body) {//Modify The Sizechar[] 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 rtg ="[size="; ltg ="[/size]";if (strBody.IndexOf(rtg) != -1 && strBody.IndexOf(ltg) != -1) {// cut the tags and text in tags and put them in other string modBody = strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg) + ltg.Length));// modify the tags and replace them with html tags modBody = modBody.Replace("[size=","<font size="); modBody = modBody.Replace("[/size","</font"); modBody = modBody.Replace("]",">");// place the new modified string in the sb variable sb.Replace(strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg) + ltg.Length)), modBody); strBody = sb.ToString();//break; } } Body = strBody;return Body; }

what happens that if i used the break keyword it works fine with the first line, but when i disaple it it doesnt work fine with loop, here's the error that occurs

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 69: {Line 70: // cut the tags and text in tags and put them in other stringLine 71: modBody = strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg)));Line 72: Line 73: // modify the tags and replace them with html tags

and here's the original text which i'm trying to modify

[size= 7]just new function[/ size]
[size= 5]all new body function[/ size]
[color= #A0522D]Color Text[/ color]
[size= 3]the one body function[/ size]


Here:

modBody = strBody.Substring(strBody.IndexOf(rtg), (strBody.IndexOf(ltg)));

The value of the strBody.Index(ltg) must > 0.

The value of the ltg is"[/size]".

The value of thestrBody is "[size= 7]just new function[/ size]".

Please check it carefully. There is a blank between the / and the size.

So the value of the strBody.IndexOf(ltg) is -1. It cause the error.

Hope it helps.


i tried to do that but it gives me the same error, and the blank i intended to leave it here because of this editor
check out this link you will find the it works fine with the 1st line

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


Sorry, it's my mistake.

But I also think this error is cause by theParameter of the method Substring.

So please check the value of these variable before the error occurred.


Here's its i made itSmile
private string ModifyBody(string Body) {/* Find out about regular expressions (regex) */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();//--cmt:7cba038b-7874-4abd-87d8-e26365a1e621--//if (strBody.IndexOf(") != -1 && strBody.IndexOf("[/size">") != -1) { rtg ="[size="; ltg ="";int start = strBody.IndexOf(rtg);// subtract the value of end tag from the start tag to select the lineint end = ((strBody.IndexOf(ltg) + ltg.Length) - start);// 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("[size=","<font size="); modBody = modBody.Replace("[/size","</font"); modBody = modBody.Replace("]",">");// place the new modified string in the sb variable sb.Replace(strBody.Substring(start, end), modBody); strBody = sb.ToString();//break; }//if (strBody.Length > 80) //{ // modBody = strBody.Substring(34, 36); // strBody = modBody; //} }return strBody; }

good

My suggestion: you can use regular expression to optimize it.

0 comments:

Post a Comment