1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-14 23:13:24 +01:00
Radarr/NzbDrone.Common/StringExtensions.cs

56 lines
1.6 KiB
C#
Raw Normal View History

2013-08-18 03:52:56 +02:00
using System;
using System.Globalization;
using System.Linq;
2013-07-24 07:35:32 +02:00
using System.Text;
using System.Text.RegularExpressions;
namespace NzbDrone.Common
{
public static class StringExtensions
{
2013-08-18 03:52:56 +02:00
public static string NullSafe(this string target)
{
2013-08-18 03:52:56 +02:00
return ((object)target).NullSafe().ToString();
}
2013-08-18 03:52:56 +02:00
public static object NullSafe(this object target)
{
2013-08-18 03:52:56 +02:00
if (target != null) return target;
return "[NULL]";
}
2013-08-18 03:52:56 +02:00
public static string FirstCharToUpper(this string input)
{
return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}
2013-08-18 03:52:56 +02:00
public static string Inject(this string format, params object[] formattingArgs)
{
return string.Format(format, formattingArgs);
}
2013-08-18 03:52:56 +02:00
private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled);
public static string RemoveAccent(this string text)
{
2013-08-18 03:52:56 +02: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 03:52:56 +02:00
public static string CleanSpaces(this string text)
{
2013-08-18 03:52:56 +02:00
return CollapseSpace.Replace(text, " ").Trim();
}
}
}