Skip to main content

Sitecore StringUtil Class - Part 1

While playing around with the Reflector today,  I chanced about the StringUtil class inside the Kernel.
If you are working with a lot of links in Sitecore, you would have encountered the function EnsurePrefix, that takes in a char and a string as arguments; this is one of the members of this class. Aside from this uber useful utility here are a list of what you can also pick up and use inside this sealed class.

Function/Method Parameters Description
ArrayToString Array, char Makes strings out of Arrays
Capitalize string Uppercases the first letter of a string and lowercases the remaining letters.
Clip string, int, bool Clips a string at a certain length. The bool argument is for the ellipsis
Combine object, object, string Combines the ToString() representation of the two objects separated by a string divider
Concat object[] Accepts a collection of objects and concats the ToString() representation of each value
Contains string, string[] Looks for the first instance of the string in the string[] object. Returns bool
CutUp string, int Cuts up the supplied string into the chunk size (int) and returns as an ArrayList
Decode byte[] Calls the UTF8.GetString(bytes)
DecodeMessageParameter string Replaces the "_eq_" with "=" and "_qst_" with "?"
DictionaryToNameValues SafeDictionary Returns a NameValueCollection from the dictionary object
Divide string, char, bool Divides the string into two based on the first index of the char separator (if bool = false) or the lastindex if true. Returns string[]
DivideAtDigit string From the supplied string, the function looks for the first character that is a digit and divides the string from there, returns a string[]
Encode string Calls UTF8.GetBytes(string)
EncodeMessageParameter string Replaces the "=" with "_eq_" and "?" with "_qst_"
EnsurePostFix char, string Ensures that the char value is at the end of the supplied string. If the supplied string is Null or Empty, the function returns the char
EnsurePreFix char, string Ensures that the char value is at the beginning of the supplied string. If the supplied string is Null or Empty, the function returns the char


Popular posts from this blog

Add Export to File Functionality in Sitecore's Search Options

One of our business users was requesting for a listing of items they've already added and have it available in a format that can be opened in a spreadsheet (Excel). My initial thought was to create a blank aspx page and write all the logic to get the data in the code-behind, run it and save the resulting file to a csv then I'm done.
But then it got me to think, it might be a better idea to have this functionality plugged in to Sitecore and made available for everyone to use.  Sitecore Bucket's Search Options fly-out seemed a good candidate for this feature (See image below). So doing some quick readings I got myself in to the "zone" and started implementing this quick and dirty PoC.

Just to explain what it actually does.

User will basically do a search, (Note that all search options require some filters or search keyword before any of the options can be used) clicks the Export to File, it pops up a dialog to confirm the action and executes, after which a browser-…

Rich Text Editor (RTE) Stripping Script tags

Ran across an issue today wherein one of our Content Editors was complaining that their page was not working, i.e. some page scripts where not firing.
Upon closer inspection, we found out that their content inside their RTE was missing.
We added them back but, it just kept getting stripped-off when we close the editor.
This did not happen to us before when we were using SC6.5, so this basically smells more like an upgrade issue.
I scanned all of the config changes (I kept a log) and found one curious setting:

<setting name="HtmlEditor.RemoveScripts" value="true" />

This setting was by default removing any embedded scripts inside the RTE.
Just turn it off in your CM and keep it on in your CD.

Add/Allow a different media URL prefix in Sitecore (aside from tilde ~) in Sitecore 7.0

We recently had a requirement when one of our external vendors required that our media URLs should not include the "~" in the link as their system could not process those correctly.
I found a few articles in the web, but most of them would suggest changing the default behavior, i.e. any "new" media item would have the 'new' replacement character though still supporting the tilde "~".
Based on the web articles, I started with the following config keys and section:

Media.RequestExtensionMedia.MediaLinkPrefixcustomHandlers We did not want to change any of the default behaviors, we just needed a way to make Sitecore support the additional URL media prefix. While sifting through the configuration, I chanced upon the following section: <mediaPrefixes>  According to the comment above it:

Allows you to configure additional media prefixes (in addition to the prefix defined by the Media.MediaLinkPrefix setting)
           The prefixes are used by Sitecore…