I Heart Strings

Brad Abrams was a founding member of the .NET common language runtime team way back in 1998. He's also the co-author of many essential books on .NET, including both volumes of the .NET Framework Standard Library Annotated Reference. I was at a presentation Brad gave to the Triangle .NET User's Group early in 2005. During the Q&A period, an audience member (and a friend of mine) asked Brad this question:

This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2006/07/i-heart-strings.html

In case you were wondering what Brad is doing these days, Brad has moved up and on from the .NET CLR team. He’s currently the group program manager for the UI Framework and Services team. His team owns ASP.NET, Atlas, and Windows Forms.

Super, super nice guy. And one of the best MS blogs out there too:


I used to like String.Format before I discovered Ruby. The {n} thing is OK until n becomes a reasonably large number (5+/-2). Then it becomes a lot harder to read.

String.Format(“My name is {0} {1} and I’m a {2}”, firstName, lastName, citizenship)


“My name is #{first_name} #{last_name} and I’m a #{citizenship}”

My favorite STL class? string. :slight_smile:

Second most favorite? vector.

“My name is #{first_name} #{last_name} and I’m a #{citizenship}”

ColdFusion has something really similar:

“My name is #first_name# #last_name# and I’m a #citizenship#”

That and grouping were about the only two things I sorta miss about using CF.

More posters:
I have these on my walls at work, and at home.

Don’t think they have 2005 yet :frowning:

They don’t have .NET 2.0 posters because they added so many classes that, just like Java, they cannot make it fit…


Java and .NET String are both a big step up from C and even from C++'s std::string, but are still fairly primitive tools compared to the string capabilities of dynamic “scripting” languages like Perl, Python, and Ruby. So its important to keep things in perspective.

String concatenation is still useful over string.format because it is an order of magnitude faster! string.format may be easier to read, but the performance difference can really show in a tight loop. Try it!

String is cool and all, but it’s /sealed/. Argh!

Jeff, you’d love what Resharper does for strings.

If you have code like this:

string s = "This " + verb + " freaking " + adjective + " I want to " + verb;

It puts a little smart tag next to it and lets you convert it to a format string. Good for cleaning up ugly string concats.

C strings (bstr, tstring, wstring, const char*, wchar*, etc.) are so durn ugly

True; I don’t think string is a native datatype in C or C++. Hard to imagine for modern languages.

My love for strings is so deep and so wide that it extends to all things string-related. I’m a huge StringBuilder fan, too.

I’m thinking of naming my first born child StringBuilder.

To John Lam… you have the same type of inline syntax in PHP, and I’m sure a few dozen other languages. Ruby is great but that feature is far from unique.

Oh, and I think a sealed Strings class is great. There is nothing more annoying than having to maintain someone else’s code and seeing a class that you think you know how works but you don’t, because it’s a funky inherited type of a base system class and they’ve overridden half of it. Blench.

Aye, I have to agree strings are wonderful, but concur with Nick. Its Sealed! This is OO days, we want to inherit and extend that functionality, not be told we can’t. Sealed, in this instance, is taking a big step backwards in my opinion. :slight_smile:

From all I’ve heard String.Concat does perform a bit better than String.Format and a lot better than “a”+“b”+“c” for patching together long strings. Obviously String.Format has it’s own advantages in many cases but if you just want to tie a bunch of strings together concat is a good option.

How is String.Format any different than printf/sprintf that has been around for ever?

It’s not any different, it just supports a zillion more output formats. Unfortunately, it doesn’t support named value key/pair substitution as shown in Ruby and PHP via previous commenters…

Hi Shawn, I do disagree a little.

Not all of us are from the C/C++ world. I personally use Delphi.net, and I personally think OO inheritance is a great option/tool.

Just because some people love operator overloading doesn’t mean you should remove the ability to inherit from that base class. Personally we can extend a class (yes I use different names for my new classes, but I know what you are talking about) to provide more then just the base class offerings.

There are plenty of times I’ve wanted to extend from a class in .NET and been denied the means, because it’s sealed. Personally I think sealed has its place, but it seems a little overused currently on some classes in the framework. :slight_smile:

I want to second Haack on how ReSharper 2.0 cleans up string concatenation. It may new favorite feature and lets me know how much I was using " + "…

Shawn Oster said
To John Lam… you have the same type of inline syntax in PHP, and I’m sure a few dozen other languages. Ruby is great but that feature is far from unique.

PHP has an inline syntax, but it’s quirky and error prone, Ruby’s inline syntax (some other languages have that kind of syntax indeed) is cleaner and mostly error-free.