TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
james kerslake
NA
1
2.5k
StringBuilder class
Aug 20 2010 11:28 AM
Hi, this may seem like a trivial question but I want to get a full understanding of when to use this.
I am writing C sharp classes that contain methods which build up a big stream of HTML before passing it back as a String return value, which will then get put into an HTML page via a server control.
At the moment, the code is similar to
String
htmlstring =
"<form name='mailboxform' action='Default.aspx' method='post' target='_self'>"
+
"<input type=hidden name='pageaction' id='pageaction' value='messages' />"
+
"<input type=hidden name='mailaction' id='mailaction' value='' />"
+
"<input type=hidden name='mailbox' id='mailbox' value='"
+ ( (
int
) _startbox ) +
"' />"
+
"<input type=hidden name='page' id='page' value='"
+ _page +
"' />"
+
"<input type=hidden name='idlist' id='idlist' value='' />"
+
"<table cellpadding=2 cellspacing=2 border=0 width=100%>"
+
etc etc...
So I have massive big concatenation blocks, then maybe something like
while
( reader.Read() )
{
InboxMessage
message =
InboxMessage
.GetMessage( reader.GetInt64(
"MessageId"
) );
htmlstring +=
"<tr class="
+ ( ( currentmessage % 2 == 1 ) ?
"messagerow"
:
"altmessagerow"
) +
" onMouseOver='javascript:RollOverRow(this);' onMouseOut='javascript:RollOutRow(this,"
+ ( currentmessage % 2 ) +
");'>"
+
"<td class=whiteback width=15 valign=middle align=center onClick='javascript:OpenMessage("
+ message.MessageId +
");'>"
+
Which loops through, concatenating each time. So I have both in-line concatenations being used heavily, and also cross-line using +=.
I want to know the best way to do this, should I leave it as is or will I benefit from using the Stringbuilder class. I am not sure if the String concat overhead just occurs with the += concats, or with all the in-line ones too. ie Does String s = "test" + "hello" + "now"; Create five objects in memory? One for test, one for hello, one for now, and then two for the two concats?
It seems a little heavyweight to use
StringBuilder
htmlstring =
new
StringBuilder
(
"<table cellpadding=2 cellspacing=2 border=0 width=100% height=100%>"
,5000);
htmlstring.Append(
"<tr>"
);
htmlstring.Append(
"<td valign=top nowrap width=200 align=center>"
);
htmlstring.Append(
"<img width=180px src='images/avatars/"
+ _user.AvatarPicture +
"' />"
);
htmlstring.Append(
"</td>"
);
htmlstring.Append(
"<td width=100% nowrap valign=top>"
);
htmlstring.Append(
"<table cellpadding=2 cellspacing= border=0 width=100% height=100%>"
);
htmlstring.Append(
"<tr>"
);
htmlstring.Append(
"<td valign=top class=contentlarge>"
+ _user.UserName +
"</td>"
);
I'd appreciate anyones advice on this
Reply
Answers (
3
)
how to call the server side code is call for ajax modelpopup contrl events
search database and display in datagrid view