Radarr/NzbDrone.Common/StringExtensions.cs

56 lines
1.6 KiB
C#
Raw Normal View History

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