string concatenation

L

Locia

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(env).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(env).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?
 
J

Jon Skeet [C# MVP]

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.)
 
L

Locia

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?
 
J

Jon Skeet [C# MVP]

Locia said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top