Use String.Format (C# and VB.NET) instead of Chopping Strings
String.Format is just so cool. It makes your code less spaghetti like and makes it easier to replace string values or add new values. It also allows you to see from a quick glance what your code does. It also makes it MUCH easier to update your string and add more parameters to it. It works similarly to the printf function in C++ that takes a string, and then is followed by parameters that specify what to plug into the string.
Here is a String.Format example (C#):
text1.Text = string.Format("<a href=\"Mylink.aspx?abc=0&Color={0}\">my Link text</a>", myColor);
Isn’t that much nicer than our not so clean string concatenation (C#):
text1.Text = "<a href=\"Mylink.aspx?abc=0&Color=" + myColor + "\">my Link text</a>";
Now if we want to add one more parameter, it’s still so clean! Note that it is the same syntax for C# and VB.NET
text1.Text = string.Format("<a href=\"Mylink.aspx?abc=0&ID={0}&wl={1}\">my Link text</a>", myColor, myWeight);
However, there are some performance considerations to think about. In most cases, the minor performance gain is negligible compared to the clarity of code benefits.
Related Reading:
Other Interesting Posts
3 Responses to Use String.Format (C# and VB.NET) instead of Chopping Strings
Leave a Reply Cancel reply
-
Articles
- January 2011
- April 2010
- March 2010
- February 2010
- January 2010
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- February 2009
- December 2008
- November 2008
- October 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
-
Meta








Take a look at how much more readable your code is is after using string.format. This code takes a raw phone number and adds brackets to it for formatting (VB.NET example)
Before:
‘strPhone = “(” & strPhone.Substring(0, 3) & “)” & strPhone.Substring(3, 3) & _
‘ “-” & strPhone.Substring(6, 4)
After:
strPhone = string.Format(“({0}){1}-{2}”, strPhone.Substring(0, 3), strPhone.Substring(3, 3), strPhone.Substring(6, 4))
Take a look at the performance String.Format at: http://blogs.msdn.com/ricom/archive/2004/03/12/88715.aspx
In summary, ”
Even though it’s the worst performing, and we knew that much in advance, both of your CLR Performance Architects concur that #2 [Using String.Format] should be the default choice. In the highly unlikely event that it becomes a perf problem the issue is readily addressable with only modest local changes. Normally you’re just cashing-in on some nice maintainability. We do not alter our choice given the data below”
I’ve been reading this guide and it seems pretty straight forward. Seems like a sweet way of backing up all your Wii games to a hard drive so that you don’t have to keep putting the discs in. The laser in my drive has been failing so it seems like a pretty good option.
How to Back Up and Play Your Wii Games from an External Hard Drive
Has anyone actually tried this? Is there any chance of breaking my Wii forever? For those who have already tried it, did you get banned from playing online?