With default settings, you will get an hyphenized, lowercase, alphanumeric version of any string you please, with any diacritics removed and collapsed whitespace.
In example, having:
a ambição cerra o coração
You'll get:
a-ambicao-cerra-o-coracao
Installation
You can get the Slugify NuGet package by running the following command in the Package Manager Console:
PM> Install-Package Slugify
Basic Usage
It's really simple! Just instantiate SlugHelper and call its GenerateSlug with the string you want to convert; it'll return the URL Safe version:
using Slugify;
public class MyApp
{
public static void Main()
{
SlugHelper helper = new SlugHelper();
String title = "OLA ke ase!";
String slug = helper.GenerateSlug(title); // "ola-ke-ase"
Console.WriteLine(slug);
}
}
Configuration
You can provide a SlugHelper.Config instance to SlugHelper's constructor to customize the helper's behavior, like so:
// Creating a configuration object
SlugHelper.Config config = new SlugHelper.Config();
// Replace spaces with a hyphens
config.CharacterReplacements.Add(" ", "-");
// We want a lowercase Slug
config.ForceLowerCase = true;
// Colapse consecutive whitespace chars into one
config.CollapseWhiteSpace = true;
// Remove everything that's not a letter, number, hyphen, dot, or underscore
config.DeniedCharactersRegex = @"[^a-zA-Z0-9\-\._]";
// Create a helper instance with our new configuration
SlugHelper helper = new SlugHelper(config);
In fact, last values are so common they're the default ones! So last code could be rewritten as:
SlugHelper.Config config = new SlugHelper.Config();
SlugHelper helper = new SlugHelper(config);
One more thing: SlugHelper.Config is used when you call the parameterless SlugHelper constructor. Then ...
SlugHelper helper = new SlugHelper();
... is the same as running the code we had in first place.
Options
CharacterReplacements
Type: Dictionary<String, String>. Default: [" ": "-"].
Will replace the specified keys with their associated value.
By default, will replace spaces with hyphens.
ForceLowerCase
Type: Boolean. Default: true.
Setting it to true will convert output string to be LowerCase. If false, original casing will be preserved.
CollapseWhiteSpace
Type: Boolean. Default: true.
Setting it to true will replace consecutive whitespace characters by just one space (" ").
DeniedCharactersRegex
Type: String. Default: [^a-zA-Z0-9-._].
Any character matching this Regular Expression will be deleted from the resulting string.
License
The MIT License (MIT)
Copyright (c) 2013 Federico Cingolani
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.