2013-03-31 22:25:39 +02:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Linq;
|
|
|
|
|
2013-02-17 20:19:38 +01:00
|
|
|
namespace NzbDrone.Common.EnsureThat
|
|
|
|
{
|
2013-03-31 22:25:39 +02:00
|
|
|
public static class StringExtensions
|
2013-02-17 20:19:38 +01:00
|
|
|
{
|
2013-03-31 22:25:39 +02:00
|
|
|
public static string Inject(this string format, params object[] formattingArgs)
|
2013-02-17 20:19:38 +01:00
|
|
|
{
|
|
|
|
return string.Format(format, formattingArgs);
|
|
|
|
}
|
|
|
|
|
2013-03-31 22:25:39 +02:00
|
|
|
public static string Inject(this string format, params string[] formattingArgs)
|
2013-02-17 20:19:38 +01:00
|
|
|
{
|
2013-03-31 22:25:39 +02:00
|
|
|
return string.Format(format, formattingArgs.Cast<object>());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly Regex InvalidCharRegex = new Regex(@"[^a-z0-9\s-]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
|
|
|
|
|
|
|
public static string ToSlug(this string phrase)
|
|
|
|
{
|
|
|
|
phrase = phrase.RemoveAccent().ToLower();
|
|
|
|
|
|
|
|
phrase = InvalidCharRegex.Replace(phrase, string.Empty);
|
|
|
|
phrase = CollapseSpace.Replace(phrase, " ").Trim();
|
|
|
|
phrase = phrase.Replace(" ", "-");
|
|
|
|
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string RemoveAccent(this string txt)
|
|
|
|
{
|
|
|
|
var bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
|
|
|
|
return System.Text.Encoding.ASCII.GetString(bytes);
|
2013-02-17 20:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|