Coding Horror

programming and human factors

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:

What's your favorite class in the .NET 1.1 common langauge runtime?

His answer? String.

And that's from a guy who will forget more about the .NET runtime than I will ever know about it. I still have my .NET class library reference poster, autographed by Brad right next to the String class.

I've always felt that string is the most noble of datatypes. Computers run on ones and zeros, sure, but people don't. They use words, sentences, and paragraphs to communicate. People communicate with strings. The meteoric rise of HTTP, HTML, REST, serialization, and other heavily string-oriented, human-readable techniques vindicates -- at least in my mind -- my lifelong preference for the humble string.

Or, you could argue that we now have so much computing power and bandwidth available that passing friendly strings around in lieu of opaque binary data is actually practical. But don't be a killjoy.

Guess what my favorite new .NET 2.0 feature is. Go ahead. Guess! Generics? Nope. Partial classes? Nope again. It's the String.Contains method. And I'm awfully fond of String.IsNullOrEmpty, too.

What I love most about strings is that they have a million and one uses. They're the swiss army knife of data types. Regular expressions, for example, are themselves strings, as is SQL.*

Regex.IsMatch(s, "<[a-z]|<!|&#|Won[a-z]*s*=|(scripts*:)|expression(")

One of the classic uses for strings, going way back to the C days, is to specify output formats. Here's an example of basic string formatting in .NET.

"Date is " + DateTime.Now.ToString("MM/dd hh:mm:ss");

You can explicitly use the String.Format method to format, well, almost anything, including our date:

String.Format("Date is {0:MM/dd hh:mm:ss}", DateTime.Now);

As Karl Seguin points out, String.Format is a superior alternative to naive string concatenation:

Surely, I can't be the only one that has a hard time writing and maintaining code like:

d.SelectSingleNode("/graph/data[name='" + name + "']");

When I do write code like the above, I almost always forget my closing quote or square bracket! And as things get more complicated, it becomes a flat out nightmare.

The solution is to make heavy use of string.Format. You'll never EVER see me use plus to concatenate something to a string, and there's no reason you should either. To write the above code better, try:

d.SelectSingleNode(string.Format("/graph/data[name='{0}']", name));

It's a win-win scenario: you get more power and more protection. For a complete rundown of the zillion possible String.Format specifiers, try these links:

String class, you complete me.

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Find me here: https://infosec.exchange/@codinghorror