473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

string concatenation

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
3 2405
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
3
by: John Ford | last post by:
For simple string concatenation, is there a difference between... Dim s As String s += "add this to string" ....and... Dim s As String s = String.Concat(s, "add this to string")
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
16
by: Mark A. Sam | last post by:
Hello, I am having a problem with imputting into a string variable: Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany, txtPhone, txtEmail, txtComment, chkGrower,...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.