String Concatenation
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
If you are going to concat many string then use StringBuilder class as it will perform better. In my opinion String.format is better option as the code is more readable. By the way, String.Format uses StringBuilder under the hood.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
I'd go with
string.Concat
.string key = string.Concat("test", "::", "test");
Navaneeth How to use google | Ask smart questions
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
try STRINGBUILDER function for that
nelsonpaixao@yahoo.com.br trying to help & get help
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
If that's all you're doing, I'd stick with method 1. (And let the compiler handle.) I often use method 2 when concatenating literal text with variables; I find it easier to read (when a lot of space is added). But if you're doing a bunch of concatenations in a loop (to produce one value), use StringBuilder. Also look into String.Join
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
I believe the 2nd example uses a
StringBuilder
internally, and for large strings, with multiple instances of addition, it's more efficient. I almost always usestring.Format
over "string + string
"."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
-
What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!
Charith Jayasundara
Your method 1 is absolutely the most efficient. It concatenates the strings while compiling, so the only work done at runtime is allocating a reference of an already existing string to the variable. I suppose that you meant to concatenate string values from variables, not literal strings, so there is actually something done at runtime: Method 1:
string key = str1 + "::" + str2;
Method 2:string key = string.Format("{0}::{1}", str1, str2);
Method 1 is still the most efficient. It will compile into a call to theString.Concat(string, string, string)
method, which adds the lengths of the strings, allocates a string with that size, and copies the data from the strings into the new string. Method 2 uses aStringBuilder
, which is overkill when you just want to concatenate three strings. It calls theAppendFormat
method which has to parse the format string to know what to do with the strings. Using theString.Format
method often produces more readable code, and it can certainly do a lot more than just concatenating strings, but for your specific question it's not the most efficient.Despite everything, the person most likely to be fooling you next is yourself.
modified on Thursday, January 1, 2009 9:04 AM
-
I believe the 2nd example uses a
StringBuilder
internally, and for large strings, with multiple instances of addition, it's more efficient. I almost always usestring.Format
over "string + string
"."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
with multiple instances of addition, it's more efficient
That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.
Despite everything, the person most likely to be fooling you next is yourself.
-
John Simmons / outlaw programmer wrote:
with multiple instances of addition, it's more efficient
That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.
Despite everything, the person most likely to be fooling you next is yourself.
Guffa wrote:
John Simmons / outlaw programmer wrote: with multiple instances of addition, it's more efficient That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.
And I told him *when* using
string.Format
is more efficient."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001