1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00
Sonarr/NzbDrone.Common/StringExtensions.cs

56 lines
1.6 KiB
C#
Raw Normal View History

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;
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-08-18 04:52:56 +03:00
return ((object)target).NullSafe().ToString();
}
2013-08-18 04:52:56 +03:00
public static object NullSafe(this object target)
{
2013-08-18 04:52:56 +03:00
if (target != null) return target;
return "[NULL]";
}
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-08-18 04:52:56 +03:00
public static string Inject(this string format, params object[] formattingArgs)
{
return string.Format(format, formattingArgs);
}
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-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-08-18 04:52:56 +03:00
public static string CleanSpaces(this string text)
{
2013-08-18 04:52:56 +03:00
return CollapseSpace.Replace(text, " ").Trim();
}
}
}