Showing posts with label breaks. Show all posts
Showing posts with label breaks. Show all posts

Wednesday, March 28, 2012

string.remove and while

I have the following method that should remove html comments. But it's not reacting the way I expect. When the loop breaks and I return the string, I get the correct value - all comments are removed. But the loop will not break using neither the while conditional statement or the internal IndexOf check, therefore stuck in an infinite loop (save for the counter that breaks it).

What am I missing??

public static string RemoveComments(string page) {int i = 0;while (page.IndexOf("<!--") > 0) {int _CommentStart = page.IndexOf("<!--");int _CommentEnds = page.IndexOf("-->");if (_CommentStart > 0) {if (_CommentEnds <= _CommentStart) {break; } page.Remove(_CommentStart, (_CommentEnds - _CommentStart)); }if (page.IndexOf("<!--") < 1) { i = 100; } i++;if (i > 10)break; }return page +" " + i.ToString(); }
I'm not following that logic too well - I don't use c# either - but wouldn't this be a case for regular expressions ?
I'm not a regular expressions expert, but I'm sure there is a way to look for that string and remove it.
Do a search on using regular expressions...

Thank you, very good suggestion. I used the following:

System.Text.RegularExpressions.Regex r =new System.Text.RegularExpressions.Regex(@."\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>");return r.Replace(page,string.Empty);

Monday, March 26, 2012

StringBuilder AppendLine dont print Line breaks in my emails!

Hello... I'm working with StringBuilder to create a message to send via text email.

My methos is:

StringBuilder retorno = new StringBuilder();

retorno.AppendLine ( "A lot of text 1" );
retorno.AppendLine ( "A lot of text 2" );

return retorno.ToString() ;

Well.. I this last line I return the text to the message, but the email arrives without line breaks.

What can i do? thanks!

if you email format is in HTML, then you can use tag "<BR>" to create a line break

StringBuilder sb = new StringBuilder();
sb.Append("A lot of text 1");
sb.Append("<br>");
sb.Append("A lot of text 2);
return retorno.ToString() ;

else

you may try using \r\n, sb.Append("XXX\\r\\nXXXX");


have you tried for this one ?? it shoud work as it worked for me..

 StringBuilder retorno =new StringBuilder(); retorno.AppendLine("A lot of text 1"); retorno.AppendLine("<br />"); //....append a <br /> Tag... retorno.AppendLine("A lot of text 2"); Response.Write(retorno.ToString());

hope it helps./.