Reusing Sitecore API features

While using Sitecore, its nice to know some of Sitecores own built in utility classes.

Check out some of these classes before writing you own methods thats does the same.

 

The following is a summary of some of the best methods from Sitecore.StringUtil

  • Capitalize(string text) - makes the first letter in an text Uppercase
  • Clip(string text, int length, bool ellipsis) - Clips a text at a given point, and adds ... if elipsis is specified
  • CutUp(string text, int chunkSize) - Cuts up a string into chunks, returning an ArrayList
  • EnsurePostfix(char postfix, string value) - Ensures that a string has a given postfix, by adding the charvalue, if it isnt the postfix
  • EnsurePrefix(char prefix, string value) - Same as above, just with prefix
  • ExtractParameter(string name, string parameters) - Gives you a value of a named parameter in a parameterlist, souch as a QueryString
  • GetSizeString(long size) - Returns you the size in KB, MB or GB
  • IsWhiteSpace(string s) - checks wheather the string is a whitespace
  • NameValueToString(NameValueCollection values, string divider) - splits an NameValueCollection into one string, with the divider
  • RemoveLineFeeds(string text) - removes \r and \n
  • RemoveTags(string text) - Removes all tags in text
  • Repeat(string s, int nCount) - repeats a gives string nCount times

... and many more, ive attached the StringUtil.cs from Sitecore 6.3.1 to this article, it contains many great functions that are very usefull in a normal Scenario.

 

Other Sitecore Util classes includes:

  • Sitecore.DateUtil - contains methods for handeling dates, i.e. for converting between Sitecores ISO date format to .NET datetime, and back again
  • Sitecore.MainUtil - Many functions, but mostly hardcore Sitecore functions :-)
  • UIUtil - functions for the UI, very usefull when working with Sitecores XAML applications, but it also has functions like IsIE and IsFireFox
  • WebDAVUtil - functiosn for WebDAV integration

Feel free to share in the comments, if you have found som other great functions built into Sitecores API, that we can use.