String.Trim() fixed in .NET 4.0

A long time ago, I wrote a blog post about the problems with String.Trim(). I’m happy to see that all three issues have been addressed in the .NET Framework 4.0.

To start with, Trim() will now be consistent with Char.IsWhiteSpace(). Theoretically, this is a breaking change, but I don’t expect many programs to have a problem with this change. Note that the change is very well documented in the online help.

Secondly, the code of Trim() has been cleaned up considerably. A string that consists entirely of whitespace is no longer scanned twice. I haven’t done any benchmarks, but I expect the performance to be at least as good as for the same function in .NET 2.0 – 3.5.

Last but not least, the frequent abuse of the Trim() function to simply validate strings will greatly decrease with the introduction of the static IsNullOrWhitespace(string value) function, which is much faster than calling Trim().

It’s a small detail, compared to all the other goodies .NET 4.0 brings, but a good addition to the toolbox nonetheless.


Comments

April 13. 2010 07:41 AM

how to write an essay

Thank you, you have gained a new fan, resources like the one you mentioned here will be very useful to me. I will post a link to this page on my blog. I am sure my visitors will find that very useful, please check out my site sometime and leave me a comment

how to write an essay

April 13. 2010 10:56 PM

22 lcd monitor

I was just going to relax and read this morning then in the meantime I got your blog to read. Really good one.

22 lcd monitor

April 15. 2010 03:50 AM

cause and effect essay

Very informative posts, glad I found this site.  Thanks!

cause and effect essay

April 15. 2010 08:12 AM

bedding crib

Your blog appears quite informative. Can you please tell me how can I read your rss blog?

bedding crib

April 15. 2010 03:08 PM

kapadokya

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

kapadokya

April 21. 2010 09:27 PM

plaka sorgulama

Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

plaka sorgulama

April 23. 2010 07:57 PM

fallar fal bakma

Thanks a lot for enjoying this beauty article with me. I am apreciating it very much! Looking forward to another great article. Good luck to the author! all the best!

fallar fal bakma

April 28. 2010 08:51 AM

thy bilet fiyatları

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information

thy bilet fiyatları

May 6. 2010 05:28 PM

Rings at bargain prices

Excellent post.I want to thank you for this informative read, I was also getting the same problem in .net

Its just the information I was looking for.

Thanks mate Smile

Rings at bargain prices

May 12. 2010 11:12 AM

watches

Very, very useful. Well-written. I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

watches

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading