Formatting Strings
-
Hi all Can anyone suggest me which way of string replacment is much better ?? 1) string str = "Date today is " + Date.Now.ToString() + " !!!!" Or 2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString()) Thanxs Maharishi Bhatia
-
Hi all Can anyone suggest me which way of string replacment is much better ?? 1) string str = "Date today is " + Date.Now.ToString() + " !!!!" Or 2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString()) Thanxs Maharishi Bhatia
Never concatenate strings using
+
- it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings. UseString.Concat
orString.Format
like you're doing, but don't useToString
. What do you thingString.Format
is for? Lets say you wanted to format the date using the long date format. Just use the following:string s = string.Format("The date is {0:D}.", DateTime.Now);
If you use
ToString
, you loose that ability (unless you use the overload forDateTime.ToString
which takes a string and optionally a different culture'sIFormatProvider
. I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.Microsoft MVP, Visual C# My Articles
-
Never concatenate strings using
+
- it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings. UseString.Concat
orString.Format
like you're doing, but don't useToString
. What do you thingString.Format
is for? Lets say you wanted to format the date using the long date format. Just use the following:string s = string.Format("The date is {0:D}.", DateTime.Now);
If you use
ToString
, you loose that ability (unless you use the overload forDateTime.ToString
which takes a string and optionally a different culture'sIFormatProvider
. I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.Microsoft MVP, Visual C# My Articles
-
bloody hell never knew it was expensive to use ToString() ! Well you learn something everyday
It isn't expensive to use
ToString
(unless the implementation is crappy), I said it was expensive to concat strings using+
. UsingToString
in the variable argument list forString.Format
(and the like) is not only unnecessary but in many cases dumb since you loose the ability to apply custom formats (unless you pass the format specifiers as args to theToString
overload for theIFormattable
implementation).Microsoft MVP, Visual C# My Articles
-
Never concatenate strings using
+
- it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings. UseString.Concat
orString.Format
like you're doing, but don't useToString
. What do you thingString.Format
is for? Lets say you wanted to format the date using the long date format. Just use the following:string s = string.Format("The date is {0:D}.", DateTime.Now);
If you use
ToString
, you loose that ability (unless you use the overload forDateTime.ToString
which takes a string and optionally a different culture'sIFormatProvider
. I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: Never concatenate strings using + ...Use String.Concat C# code that 'pluses' strings together gets IL compiled down to .Concat, right? Maybe I'm misunderstanding you. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu
-
Heath Stewart wrote: Never concatenate strings using + ...Use String.Concat C# code that 'pluses' strings together gets IL compiled down to .Concat, right? Maybe I'm misunderstanding you. --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu
For the Microsoft C# compiler, yes, and under certain circumstances - which typically warrant
String.Format
for better control. Depending on the operands you're passing, the compiler doesn't always optimize usingString.Concat
either.Microsoft MVP, Visual C# My Articles
-
For the Microsoft C# compiler, yes, and under certain circumstances - which typically warrant
String.Format
for better control. Depending on the operands you're passing, the compiler doesn't always optimize usingString.Concat
either.Microsoft MVP, Visual C# My Articles
Well then, if you take into account that it's almost always optimized into
String.Concat
, then wouldn't you say that using + is actually much quicker than usingString.Format
, which involves parsing and appending? From a readibility stand point, the + operator is superior in my opinion. Now, if special formatting is necessary, then theFormat
method is available -- for formatting purposes. Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
-
Hi all Can anyone suggest me which way of string replacment is much better ?? 1) string str = "Date today is " + Date.Now.ToString() + " !!!!" Or 2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString()) Thanxs Maharishi Bhatia
- string str = "Date today is " + Date.Now + " !!!!"; ToString() is automatically called for you. :-) Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
-
Well then, if you take into account that it's almost always optimized into
String.Concat
, then wouldn't you say that using + is actually much quicker than usingString.Format
, which involves parsing and appending? From a readibility stand point, the + operator is superior in my opinion. Now, if special formatting is necessary, then theFormat
method is available -- for formatting purposes. Regards, Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
It depends on the circumstances, I'd say. Like you said, for special formatting using
String.Format
is definitely better, though you could accomplish this task with+
orString.Concat
if you use the overloadedToString
that corresponds to theIFormattable
implementation.Microsoft MVP, Visual C# My Articles