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

String.Concat vs String.Format

P: n/a
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu
Jun 9 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
If you think a bit about it you 'll get the answer.

String.Concat is made to do exactly this where it get the name from, whereas
String.Format can do lots of stuff and has to scan the string for format
specifiers first.
"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag
news:%2******************@TK2MSFTNGP04.phx.gbl...
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu

Jun 9 '06 #2

P: n/a
I realize that but does .net do any compiler optimizations if
string.Format is used?

- ramadu

:
If you think a bit about it you 'll get the answer.

String.Concat is made to do exactly this where it get the name from, whereas
String.Format can do lots of stuff and has to scan the string for format
specifiers first.
"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag
news:%2******************@TK2MSFTNGP04.phx.gbl...
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu


Jun 9 '06 #3

P: n/a
Hello ramadu,

If u use reflector you can see that String.Format use StringBuilder class
and calls its AppendFormat method that is pretty rich.
String,Contat uses string.FastAllocateString and FillStringChecked methods

I'd assume that String.Concat is more fastest case

r> I know its a sin to use strings, lets skip that part...
r>
r> Which of these is faster and uses less memory?
r>
r> String.Format("SomeValue='{0}'", m_Value);
r>
r> or
r>
r> String.Concat("SomeValue='", m_Value, "'");
r>
r> - ramadu
r>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Jun 9 '06 #4

P: n/a
But in that case String.Concat will be costly in terms of memory as it
will create multiple immutable strings...

- ramadu

:
Hello ramadu,

If u use reflector you can see that String.Format use StringBuilder
class and calls its AppendFormat method that is pretty rich.
String,Contat uses string.FastAllocateString and FillStringChecked methods

I'd assume that String.Concat is more fastest case

r> I know its a sin to use strings, lets skip that part...
r> r> Which of these is faster and uses less memory?
r> r> String.Format("SomeValue='{0}'", m_Value);
r> r> or
r> r> String.Concat("SomeValue='", m_Value, "'");
r> r> - ramadu
r> ---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

Jun 9 '06 #5

P: n/a
Just for the sake of discussion. All your variables has a scope. How many
variable can you accumulate till you quit the program? So, to me I would say
I use whichever way I feel is comfortable to me.

chanmm

"ramadu" <tn*@newsgroups.nospam> wrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu

Jun 10 '06 #6

P: n/a
ramadu <tn*@newsgroups.nospam> wrote:
I know its a sin to use strings, lets skip that part...
In what way? Using strings is part of every day life. Is there some
particular use you're thinking of which isn't a good idea, like using
values directly in SQL statements?
Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");


Well, the Concat call is faster, *but* it's unlikely to ever be
significant. The more important question is "Which of these is clearer,
and more readable?" There, the answer is String.Format IMO. On the
other hand, your Concat call could be made easier to read too:

string x = "SomeValue='" + m_Value + "'";

That will compile to the same code, and is fairly readable. I'd still
use String.Format though, I think.

--
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
Jun 10 '06 #7

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");


Well, the Concat call is faster, *but* it's unlikely to ever be
significant. The more important question is "Which of these is clearer,
and more readable?" There, the answer is String.Format IMO.


I don't know -- when you're just concatenating strings, + (or Concat) is
more readable, IMO.

///ark
Jun 10 '06 #8

P: n/a
what do you mean with "compiler optimizations"?

"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag
news:44**************@newsgroups.nospam...
I realize that but does .net do any compiler optimizations if string.Format
is used?

- ramadu

:
If you think a bit about it you 'll get the answer.

String.Concat is made to do exactly this where it get the name from,
whereas String.Format can do lots of stuff and has to scan the string for
format specifiers first.
"ramadu" <tn*@newsgroups.nospam> schrieb im Newsbeitrag
news:%2******************@TK2MSFTNGP04.phx.gbl...
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu


Jun 10 '06 #9

P: n/a
The Concat would be slightly faster and memory efficient, but if you are
trying to optimize your code you are probably looking in the wrong place.

What are you going to use the string for?

ramadu wrote:
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu

Jun 10 '06 #10

P: n/a
Mark Wilden wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

Well, the Concat call is faster, *but* it's unlikely to ever be
significant. The more important question is "Which of these is clearer,
and more readable?" There, the answer is String.Format IMO.


I don't know -- when you're just concatenating strings, + (or Concat) is
more readable, IMO.

///ark


I think it depends on what you are doing:

version #1:

string sql = "";
sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
+ m_snoopy + "', '" + m_bugs + "')";

version #2

// this string would probably be defined somewhere else
string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values ({0}, {1}, '{2}', '{3}', '{4}')";

string sql = String.Format(sqlIns, m_custid, m_sal,
m_bar, m_snoopy, m_bugs);

I think version #2 is the cleanest.

Jim
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You roll an 18 in Dex and see if you
don't end up with a girlfriend
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
JimD
Central FL, USA, Earth, Sol
Jun 11 '06 #11

P: n/a
"JimD" <Ji*@keeliegirl.dyndns.org> wrote in message
news:Ji******************@tornado.tampabay.rr.com. ..
// this string would probably be defined somewhere else
string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values ({0}, {1}, '{2}', '{3}', '{4}')";

string sql = String.Format(sqlIns, m_custid, m_sal,
m_bar, m_snoopy, m_bugs);

I think version #2 is the cleanest.


I agree, but that isn't concatenation.

///ark
Jun 11 '06 #12

P: n/a
Mark Wilden <Ma********@newsgroups.nospam> wrote:
"JimD" <Ji*@keeliegirl.dyndns.org> wrote in message
news:Ji******************@tornado.tampabay.rr.com. ..
// this string would probably be defined somewhere else
string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values ({0}, {1}, '{2}', '{3}', '{4}')";

string sql = String.Format(sqlIns, m_custid, m_sal,
m_bar, m_snoopy, m_bugs);

I think version #2 is the cleanest.


I agree, but that isn't concatenation.


Version 1 was though. You said:

"I don't know -- when you're just concatenating strings, + (or Concat)
is more readable, IMO."

Tha alternative to the Format call above was:

string sql = "";
sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
+ m_snoopy + "', '" + m_bugs + "')";

That's just concatenating strings, which would suggest that you'd
prefer that to the Format version.

--
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
Jun 11 '06 #13

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
string sql = "";
sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
+ " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
+ m_snoopy + "', '" + m_bugs + "')";

That's just concatenating strings


Yes, you're right.

When pasting two strings together, I think + is simplest and clearest. When
formatting a long mixture of string constants and string variables, I think
Format is simpler. People talk about the former as concatenation and the
latter as formatting.

However, it's just my personal preference; I don't think it makes too much
difference either way. I certainly wouldn't worry about Format taking a
couple more ticks to execute.

(BTW, as long as we're on the subject, your StringBuilder page has let me
astound and amaze more than one of my colleagues who shun + like vampires.)
Jun 11 '06 #14

P: n/a
Its mostly for select statements in a DataTable.

- Sri

:
The Concat would be slightly faster and memory efficient, but if you are
trying to optimize your code you are probably looking in the wrong place.

What are you going to use the string for?

ramadu wrote:
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu

Jun 11 '06 #15

P: n/a
This kind of discussion is the result of too much pre-optimization and
also trying to steadfastly adhere to such overly generalized rules as
"concatenation is bad!".

For simple log statements, or when you're concatentation is limited,
it's fine.

It's when you are concatenating strings (especially long strings) in
loops, or doing lots of editing to the string, when you should look for
alternatives.

Jun 12 '06 #16

P: n/a
"Matthew Brown" <Oc******@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
This kind of discussion is the result of too much pre-optimization and
also trying to steadfastly adhere to such overly generalized rules as
"concatenation is bad!".

For simple log statements, or when you're concatentation is limited,
it's fine.

It's when you are concatenating strings (especially long strings) in
loops, or doing lots of editing to the string, when you should look for
alternatives.


Actually, we've both read
<http://www.yoda.arachsys.com/csharp/stringbuilder.html>. :)
Jun 12 '06 #17

P: n/a
Then you shouldn't use either method. Use parameters instead.

Parameters are somewhat slower than putting the query together yourself,
but it's easier and safer. As the database call is going to use 99% of
the execution time of the routine anyway, you are looking in the wrong
place if you are trying to speed it up by optimizing the creation of the
query.

If you want to put together the queries yourself anyway, you should
escape the characters that the database uses in strings. For MySQL, for
an example, it is done like this:

"SomeValue='" + m_Value.Replace("\\","\\\\").Replace("'","\\'") + "'"

If you don't, you will encounter problems whenever someone uses any of
those characters. If you are lucky you only end up with an error
message. If you are unlucky someone used it for SQL injections, and all
your data is stolen and/or deleted.
ramadu wrote:
Its mostly for select statements in a DataTable.

- Sri

:
The Concat would be slightly faster and memory efficient, but if you
are trying to optimize your code you are probably looking in the wrong
place.

What are you going to use the string for?

ramadu wrote:
I know its a sin to use strings, lets skip that part...

Which of these is faster and uses less memory?

String.Format("SomeValue='{0}'", m_Value);

or

String.Concat("SomeValue='", m_Value, "'");

- ramadu

Jun 12 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.