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.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments