By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

string concatenation

P: n/a
I have a Template base class and another subclasses of Template.

I have a tree structure of template object.
class Template
{
ArrayList child;
public virtual String Eval(Environment env)
{
String result="";
foreach (Template child in GetChild())
{
result=result+child.Eval(env).PadLeft(child.Eval(e nv).Length);
}
return result;
}
}
class SubTemplate :Template
{
public override String Eval(Environment env)
{
.......
String result="";
foreach (Template child in GetChild())
{
result=result+child.Eval(env).PadLeft(child.Eval(e nv).Length);
}
return result;
}
}
Eval is a polymorphic method.

After I build a tree of Template I call Eval on the root and this
return a string.
I use operator + to concatenate string but I would like know what is a
efficient method to concatenate string
in this specific case?

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Locia <ro********@yahoo.it> wrote:

<snip>
After I build a tree of Template I call Eval on the root and this
return a string.
I use operator + to concatenate string but I would like know what is a
efficient method to concatenate string
in this specific case?


You should use a StringBuilder.

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for an
explanation.

I'd also strongly consider changing it to only evaluate child.Eval(env)
once per iteration, unless it's pretty much guaranteed to be a cheap
operation - I don't see that there's any need to evaluate the child
twice. (In this case, I don't see what it's actually meant to be doing
anyway - padding something to its existing length won't change it at
all.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
Thanks again Jon.
It's right, I must call Eval function once for iteration and save it.

I use this line to add a new space in the string.
child.Eval(env).PadLeft(child.Eval(env).Length);

How you add a new space in string?

Nov 17 '05 #3

P: n/a
Locia <ro********@yahoo.it> wrote:
Thanks again Jon.
No problem.
It's right, I must call Eval function once for iteration and save it.
But currently you're calling it twice. Even if you *do* need to use
PadLeft, you should do something like:

string evaluated = child.Eval(env);
evaluated = evaluated.PadLeft (evaluated.Length);
I use this line to add a new space in the string.
child.Eval(env).PadLeft(child.Eval(env).Length);
But that doesn't - it leaves it exactly as it is. From the docs for
PadLeft:

<quote - description of the totalWidth parameter>
The number of characters in the resulting string, equal to the number
of original characters plus any additional padding characters.
</quote>

Now, if the number of characters in the resulting string is going to be
the same as the number of characters in the original string, there
can't have been any padding, can there?
How you add a new space in string?


Well, that depends on how much you need to pad it by. I'm sure your
code isn't doing what you actually want it to be doing here - what
*exactly* are you trying to pad it with? If it's a single string, you
would use " "+whatever if you were using string concatenation directly,
but if you're already using a StringBuilder, just use:

builder.Append (" ");
builder.Append (whatever);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.