The rather unknown StringUtil class in Sitecore

This is just another quick tip about the Sitecore API and this one has been around forever but I rarely see it used.

I use it quite often myself when I perform URL or path manipulations so now I want to spread the knowledge so others can benefit from this helping class as well.

It is a class called StringUtil which, as the name implies, contain various methods that does something useful with strings.

It contain a lot of methods and some are more or less useful but there are some which deserves to be highlighted:

  • EnsurePrefix(char prefix, string value) – ensures that the passed prefix parameter is set as a prefix on the string value
  • EnsurePostfix(char postfix, string value) – same as above just for postfix
  • RemovePrefix(char prefix, string value) – Removes the passed prefix parameter on the string value if it exists
  • RemovePostfix(char postfix, string value) – same as above just for postfix

I use these four methods very often when concatenating strings for paths or urls, removing or ensuring a / whenever needed.

Short example taken out of context

Other useful methods in the class:

  • RemoveLineFeeds(string text) – remove linebreaks from the string text, easier to read than someString.Replace(“\r”, “”).Replace(“\n”, “”).
  • GetLastPart(string text, char delimiter, string defaultValue) – returns the last part of the string text which is delimited using the char delimiter. Returns default value if the delimiter is not found. Useful for parsing placeholder names from delta renderings.

There are quite a lot of other methods to look at in the class but I never really used them, some of them are similar or identical to methods which are now part of the .NET framework. This class is old. It dates back to at least Sitecore 5.0 where it is missing some methods but most of them are there.

I really get a bad vibe from the name StringUtil. No class should ever be called Util. The name just shows that whoever wrote the class didn’t really know the responsibility that the class would have in the end and was too lazy to finish the word utilities (laziness can otherwise be a strong quality in a developer).

Classes named like this typically ends up breaking the Single Responsibility Principle or are just completely tech focused which in the end leads to unmanageable spaghetti. In this case it is just fine though, it is legacy code which is still very useful.

Retrospectively I made a ton of Utils classes as well some 4-6 years ago so I am definitely not blaming or bashing anyone.

Back then it was common and you will often find classes like this in other API’s as well. For example in the Android API there is a class called TextUtils which is somewhat similar in that it contains common methods used on strings. If the methods was written today in C# they would ideally be written as extension methods on a string.

A couple of other bloggers have already written posts about this class before some time ago, for example see this nice post from Mikkel Holck Madsen I just found.

Anders Laub

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders has been nominated a Sitecore Technical MVP three years in a row for 2014, 2015 and 2016. Anders is now working as a Sr. Solutions Architect at Sitecore in Copenhagen.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*
Website