Replace non-english characters

I needed to replace swedish characters from string without removing the character for itemname.

With Sitecore you can use ItemUtil.ProposeValidItemName() but it removes the invalid characters.
I have a text like “Flöde” and I want it to be “Flode” and not “Flde” like Sitecores ItemUtil does.

Then I can use the following code:

byte[] b = Encoding.GetEncoding(1251).GetBytes("Flöde/");
string output = Encoding.ASCII.GetString(b);

string validItemName = ItemUtil.ProposeValidItemName(output);

________________________
output: "Flode/"
validItemName: "Flode"

The ItemUtil.ProposeValidItemName will give you a valid item name for Sitecore.
The string output would still contain possible invalid special characters that you don’t allow in the Sitecore config setting ItemNameValidation regex.

Leave a comment