473,659 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String.Concat vs String.Format

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
17 15315
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******** **********@TK2M SFTNGP04.phx.gb l...
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
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******** **********@TK2M SFTNGP04.phx.gb l...
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
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.FastAllo cateString and FillStringCheck ed 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
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.FastAllo cateString and FillStringCheck ed 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
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******** **********@TK2M SFTNGP04.phx.gb l...
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
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.co m>
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
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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
what do you mean with "compiler optimizations"?

"ramadu" <tn*@newsgroups .nospam> schrieb im Newsbeitrag
news:44******** ******@newsgrou ps.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******** **********@TK2M SFTNGP04.phx.gb l...
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
39797
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear = (String)sail_date.toString().substring(0,4);
2
2457
by: Dennis Westermann | last post by:
Hi, does anybody know, how i can attach a simple apostroph ( ' ) to a string. I want to create a string with concat(..), which starts and ends with a simple apostroph. The output should be: 'Examplestring' In my Stylesheet it looks as follows: <xsl:variable name="hochkomma" select="'''"/> <xsl:for-each select="//T_BSL_RBE_RAW">
2
1917
by: jez123456 | last post by:
Hi I'm attempting to pass a string into another string but I get an error This code work ok jro.CompactDatabase(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Compact.mdb", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\tempdb.mdb;Jet OLEDB:Engine Type=5"); However when I declare a string and try to pass that I get an error
3
485
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b & c & d & e +/- 420ms
1
1919
by: Trint Smith | last post by:
Ok, I have a webform that has these checkboxes: 1. something 2. something else 3. and something else When the user clicks on the checkbox, I want all of the selections to go into a textbox if all are checked. But currently, just the last selection is going in. Here's the code I have:
10
2624
by: Ben | last post by:
Hi I have a string which is at least 4 characters but can be more which I need to pad, with zeros, to a full length of 15 after the 3rd charactor. Eg '1234' to '123000000000004' How can I do this? Also how can I truncate this back to 4 or however many charactors (e.g.
7
7570
by: Leonel Gayard | last post by:
Hi all, I had to write a small script, and I did it in python instead of shell-script. My script takes some arguments from the command line, like this. import sys args = sys.argv if args == : print """Concat: concatenates the arguments with a colon (:) between them
3
3221
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at other. i have two functions in the same .cpp file //exception occurs at this function void call(const char* var2 ) {
15
2593
by: James | last post by:
Which is better, which is faster, which is easier etc... ????? String.Format ( "yadda {0} yadda {1}", x, y ) "yadda" + x.ToString() + " yadda" + y.tostring(); My code has a mish mash of both and I am wondewring if I should standardize or not ??
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.