473,386 Members | 1,819 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,386 software developers and data experts.

Advantage of String.format?

Why is String.format better than
just concatenating the string?

is it just the cost of creating a new string? or any thing more?
Nov 19 '05 #1
3 1744
string.format and string concatenation are two different things. You'd use
string format when you have something like:

string rootPath = "/myApplication/resources/{0}";
string resourcePath = string.Format(rootPath, "Resource.xml");

ie, when you want to format an actual string (either as I've shown or with
additional string formatting)

String concatenation is for adding two string...if you want efficient string
concatenation look at the System.Text.StringBuilder class

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"mavrick101" <ma********@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Why is String.format better than
just concatenating the string?

is it just the cost of creating a new string? or any thing more?

Nov 19 '05 #2
Since strings are immutable in .NET, concatenating strings builds another
string. This can lead to inefficiencies if done too much. String.Format uses
the StringBuilder to build the string and the StringBuilder buffers the string
for performance.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Why is String.format better than
just concatenating the string?
is it just the cost of creating a new string? or any thing more?


Nov 19 '05 #3
First, readability is the main reason to use it. Which code is easier to
read?

string s = "<input type=\"";
s += Type;
s += "\" id=\";
s += ID;
s += "\" />";

or

string s = String.Format( @"<input type=""{0}"" id=""{1}"" />", Type, ID );

As far as performance, like Brock said, String.Format using StringBuilder to
build the new string. This is fine when you are concatenating several
strings, but performance on String.Format( "hello{0}, " world" ) will be
better to use string concatenation. StringBuilders aren't free, plus the
StringBuilder still has to parse the string out.

If you are really searching for optimally fast code, you should declare a
StringBuilder and use Append to add the values. But it is a balance between
readability and performance.

System.Text.StringBuilder sb = new StringBuilder( 50 ); //note the initial
size has been set, estimate the total size.
sb.Append( "<input type=\"" );
sb.Append( Type );
sb.Append( "\" id=\" );
sb.Append( ID );
sb.Append( "\" />" );

bill
"mavrick101" <ma********@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Why is String.format better than
just concatenating the string?

is it just the cost of creating a new string? or any thing more?

Nov 19 '05 #4

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

Similar topics

2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
5
by: raffelm | last post by:
I'm struggling to find a way to include long path names in a command line argument string that I have to build at runtime. I need to create a string like -o:"c:\my documents\my file.txt". ...
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
1
by: Remi THOMAS | last post by:
Hi, When you execute this line of code string script = string.Format("foreach (GroupDoc gdoc in {0}.SetGroup(\"{1}\") {\r\n", "p1", "p2"); You get System.FormatException was unhandled...
15
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...
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.