2013-08-18 01:52:56 +00:00
|
|
|
using System;
|
2018-11-23 07:03:32 +00:00
|
|
|
using System.Collections.Generic;
|
2013-08-18 01:52:56 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
2013-07-24 05:35:32 +00:00
|
|
|
using System.Text;
|
2013-05-29 00:15:12 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
2014-12-02 06:26:25 +00:00
|
|
|
namespace NzbDrone.Common.Extensions
|
2013-05-29 00:15:12 +00:00
|
|
|
{
|
|
|
|
public static class StringExtensions
|
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
private static readonly Regex CamelCaseRegex = new Regex("(?<!^)[A-Z]", RegexOptions.Compiled);
|
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
public static string NullSafe(this string target)
|
2013-05-29 00:15:12 +00:00
|
|
|
{
|
2013-08-18 01:52:56 +00:00
|
|
|
return ((object)target).NullSafe().ToString();
|
2013-05-29 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
public static object NullSafe(this object target)
|
2013-05-29 00:15:12 +00:00
|
|
|
{
|
2019-12-22 22:08:53 +00:00
|
|
|
if (target != null)
|
|
|
|
{
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
return "[NULL]";
|
|
|
|
}
|
2013-05-29 00:15:12 +00:00
|
|
|
|
2019-06-14 03:54:25 +00:00
|
|
|
public static string FirstCharToLower(this string input)
|
|
|
|
{
|
2019-06-29 22:33:49 +00:00
|
|
|
if (string.IsNullOrEmpty(input))
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return char.ToLowerInvariant(input.First()) + input.Substring(1);
|
2019-06-14 03:54:25 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
public static string FirstCharToUpper(this string input)
|
|
|
|
{
|
2019-06-29 22:33:49 +00:00
|
|
|
if (string.IsNullOrEmpty(input))
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return char.ToUpperInvariant(input.First()) + input.Substring(1);
|
2013-08-18 01:52:56 +00:00
|
|
|
}
|
2013-05-29 00:15:12 +00:00
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
public static string Inject(this string format, params object[] formattingArgs)
|
|
|
|
{
|
|
|
|
return string.Format(format, formattingArgs);
|
2013-05-29 00:15:12 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled);
|
|
|
|
|
2014-07-20 12:14:47 +00:00
|
|
|
public static string Replace(this string text, int index, int length, string replacement)
|
|
|
|
{
|
|
|
|
text = text.Remove(index, length);
|
|
|
|
text = text.Insert(index, replacement);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
public static string RemoveAccent(this string text)
|
2013-05-30 03:26:47 +00:00
|
|
|
{
|
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-05-30 03:26:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:16:40 +00:00
|
|
|
public static string TrimEnd(this string text, string postfix)
|
|
|
|
{
|
|
|
|
if (text.EndsWith(postfix))
|
2019-12-22 22:08:53 +00:00
|
|
|
{
|
2014-04-17 23:16:40 +00:00
|
|
|
text = text.Substring(0, text.Length - postfix.Length);
|
2019-12-22 22:08:53 +00:00
|
|
|
}
|
2014-04-17 23:16:40 +00:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2019-06-14 03:54:25 +00:00
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
public static string Join(this IEnumerable<string> values, string separator)
|
|
|
|
{
|
|
|
|
return string.Join(separator, values);
|
|
|
|
}
|
2014-04-17 23:16:40 +00:00
|
|
|
|
2013-08-18 01:52:56 +00:00
|
|
|
public static string CleanSpaces(this string text)
|
2013-05-29 00:15:12 +00:00
|
|
|
{
|
2013-08-18 01:52:56 +00:00
|
|
|
return CollapseSpace.Replace(text, " ").Trim();
|
2013-05-29 00:15:12 +00:00
|
|
|
}
|
2014-02-26 05:40:47 +00:00
|
|
|
|
|
|
|
public static bool IsNullOrWhiteSpace(this string text)
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
return string.IsNullOrWhiteSpace(text);
|
2014-02-26 05:40:47 +00:00
|
|
|
}
|
2014-04-28 06:34:29 +00:00
|
|
|
|
2014-09-11 23:49:41 +00:00
|
|
|
public static bool IsNotNullOrWhiteSpace(this string text)
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
return !string.IsNullOrWhiteSpace(text);
|
2014-09-11 23:49:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:44:59 +00:00
|
|
|
public static bool StartsWithIgnoreCase(this string text, string startsWith)
|
|
|
|
{
|
|
|
|
return text.StartsWith(startsWith, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool EndsWithIgnoreCase(this string text, string startsWith)
|
|
|
|
{
|
|
|
|
return text.EndsWith(startsWith, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool EqualsIgnoreCase(this string text, string equals)
|
|
|
|
{
|
|
|
|
return text.Equals(equals, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
2014-04-28 06:34:29 +00:00
|
|
|
public static bool ContainsIgnoreCase(this string text, string contains)
|
|
|
|
{
|
|
|
|
return text.IndexOf(contains, StringComparison.InvariantCultureIgnoreCase) > -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string WrapInQuotes(this string text)
|
|
|
|
{
|
|
|
|
if (!text.Contains(" "))
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "\"" + text + "\"";
|
|
|
|
}
|
2015-10-14 02:01:47 +00:00
|
|
|
|
|
|
|
public static byte[] HexToByteArray(this string input)
|
|
|
|
{
|
|
|
|
return Enumerable.Range(0, input.Length)
|
2019-12-22 22:08:53 +00:00
|
|
|
.Where(x => x % 2 == 0)
|
2015-10-14 02:01:47 +00:00
|
|
|
.Select(x => Convert.ToByte(input.Substring(x, 2), 16))
|
|
|
|
.ToArray();
|
|
|
|
}
|
2016-02-14 16:55:32 +00:00
|
|
|
|
|
|
|
public static string ToHexString(this byte[] input)
|
|
|
|
{
|
|
|
|
return string.Concat(Array.ConvertAll(input, x => x.ToString("X2")));
|
|
|
|
}
|
2016-06-02 04:24:41 +00:00
|
|
|
|
|
|
|
public static string FromOctalString(this string octalValue)
|
|
|
|
{
|
|
|
|
octalValue = octalValue.TrimStart('\\');
|
|
|
|
|
|
|
|
var first = int.Parse(octalValue.Substring(0, 1));
|
|
|
|
var second = int.Parse(octalValue.Substring(1, 1));
|
|
|
|
var third = int.Parse(octalValue.Substring(2, 1));
|
2019-12-22 22:08:53 +00:00
|
|
|
var byteResult = (byte)((first << 6) | (second << 3) | third);
|
2016-06-02 04:24:41 +00:00
|
|
|
|
2019-12-22 22:08:53 +00:00
|
|
|
return Encoding.ASCII.GetString(new[] { byteResult });
|
2016-06-02 04:24:41 +00:00
|
|
|
}
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
public static string SplitCamelCase(this string input)
|
|
|
|
{
|
|
|
|
return CamelCaseRegex.Replace(input, match => " " + match.Value);
|
|
|
|
}
|
2019-06-14 03:54:25 +00:00
|
|
|
|
|
|
|
public static bool ContainsIgnoreCase(this IEnumerable<string> source, string value)
|
|
|
|
{
|
|
|
|
return source.Contains(value, StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
}
|
2022-01-07 03:42:32 +00:00
|
|
|
|
|
|
|
public static string EncodeRFC3986(this string value)
|
|
|
|
{
|
|
|
|
// From Twitterizer http://www.twitterizer.net/
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
var encoded = Uri.EscapeDataString(value);
|
|
|
|
|
|
|
|
return Regex
|
|
|
|
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
|
|
|
|
.Replace("(", "%28")
|
|
|
|
.Replace(")", "%29")
|
|
|
|
.Replace("$", "%24")
|
|
|
|
.Replace("!", "%21")
|
|
|
|
.Replace("*", "%2A")
|
|
|
|
.Replace("'", "%27")
|
|
|
|
.Replace("%7E", "~");
|
|
|
|
}
|
2013-05-29 00:15:12 +00:00
|
|
|
}
|
2018-10-30 20:44:59 +00:00
|
|
|
}
|