2013-08-18 04:52:56 +03:00
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
2013-07-24 08:35:32 +03:00
|
|
|
using System.Text;
|
2013-05-29 03:15:12 +03:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
public static class StringExtensions
|
|
|
|
{
|
2013-08-18 04:52:56 +03:00
|
|
|
public static string NullSafe(this string target)
|
2013-05-29 03:15:12 +03:00
|
|
|
{
|
2013-08-18 04:52:56 +03:00
|
|
|
return ((object)target).NullSafe().ToString();
|
2013-05-29 03:15:12 +03:00
|
|
|
}
|
|
|
|
|
2013-08-18 04:52:56 +03:00
|
|
|
public static object NullSafe(this object target)
|
2013-05-29 03:15:12 +03:00
|
|
|
{
|
2013-08-18 04:52:56 +03:00
|
|
|
if (target != null) return target;
|
|
|
|
return "[NULL]";
|
|
|
|
}
|
2013-05-29 03:15:12 +03:00
|
|
|
|
2013-08-18 04:52:56 +03:00
|
|
|
public static string FirstCharToUpper(this string input)
|
|
|
|
{
|
|
|
|
return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
|
|
|
|
}
|
2013-05-29 03:15:12 +03:00
|
|
|
|
2013-08-18 04:52:56 +03:00
|
|
|
public static string Inject(this string format, params object[] formattingArgs)
|
|
|
|
{
|
|
|
|
return string.Format(format, formattingArgs);
|
2013-05-29 03:15:12 +03:00
|
|
|
}
|
|
|
|
|
2013-08-18 04:52:56 +03:00
|
|
|
private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled);
|
|
|
|
|
|
|
|
public static string RemoveAccent(this string text)
|
2013-05-30 06:26:47 +03:00
|
|
|
{
|
2013-08-18 04:52:56 +03:00
|
|
|
var normalizedString = text.Normalize(NormalizationForm.FormD);
|
|
|
|
var stringBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
foreach (var c in normalizedString)
|
|
|
|
{
|
|
|
|
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
|
|
|
|
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
|
|
|
|
{
|
|
|
|
stringBuilder.Append(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
|
2013-05-30 06:26:47 +03:00
|
|
|
}
|
|
|
|
|
2013-08-18 04:52:56 +03:00
|
|
|
public static string CleanSpaces(this string text)
|
2013-05-29 03:15:12 +03:00
|
|
|
{
|
2013-08-18 04:52:56 +03:00
|
|
|
return CollapseSpace.Replace(text, " ").Trim();
|
2013-05-29 03:15:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|