2021-11-20 13:52:28 +01:00
|
|
|
using System.Globalization;
|
2020-11-26 16:13:00 +01:00
|
|
|
using System.Text.RegularExpressions;
|
2020-03-22 14:18:34 +01:00
|
|
|
|
2021-11-20 13:52:28 +01:00
|
|
|
namespace zero.Extensions;
|
2021-11-19 14:59:24 +01:00
|
|
|
|
|
|
|
|
public static class StringExtensions
|
2020-03-22 14:18:34 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
const string SPACE = " ";
|
|
|
|
|
|
|
|
|
|
static Regex replaceMultipleSpacesRegex { get; } = new Regex("[ ]{2,}", RegexOptions.None);
|
|
|
|
|
|
|
|
|
|
static Regex newLineCharsRegex { get; } = new Regex("\t|\n|\r", RegexOptions.None);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string FullTrim(this string value)
|
2020-03-22 14:18:34 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
if (String.IsNullOrEmpty(value))
|
2020-11-26 16:13:00 +01:00
|
|
|
{
|
2020-03-22 14:29:12 +01:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
return replaceMultipleSpacesRegex.Replace(value, SPACE).Trim();
|
|
|
|
|
}
|
2020-03-22 14:29:12 +01:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
|
|
|
|
|
public static string EnsureStartsWith(this string input, string toStartWith)
|
|
|
|
|
{
|
|
|
|
|
if (input.StartsWith(toStartWith)) return input;
|
|
|
|
|
return toStartWith + input.TrimStart(toStartWith);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string EnsureStartsWith(this string input, char value)
|
|
|
|
|
{
|
|
|
|
|
return input.StartsWith(value.ToString(CultureInfo.InvariantCulture)) ? input : value + input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string EnsureEndsWith(this string input, char value)
|
|
|
|
|
{
|
|
|
|
|
return input.EndsWith(value.ToString(CultureInfo.InvariantCulture)) ? input : input + value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string EnsureEndsWith(this string input, string toEndWith)
|
|
|
|
|
{
|
|
|
|
|
return input.EndsWith(toEndWith.ToString(CultureInfo.InvariantCulture)) ? input : input + toEndWith;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string EnsureSurroundedWith(this string input, char toSurroundWith)
|
|
|
|
|
{
|
|
|
|
|
return input.EnsureStartsWith(toSurroundWith).EnsureEndsWith(toSurroundWith);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string Or(this string value, string fallback)
|
|
|
|
|
{
|
|
|
|
|
return String.IsNullOrWhiteSpace(value) ? fallback : value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string TrimEnd(this string value, string forRemoving)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(value)) return value;
|
|
|
|
|
if (String.IsNullOrEmpty(forRemoving)) return value;
|
|
|
|
|
|
|
|
|
|
while (value.EndsWith(forRemoving, StringComparison.InvariantCultureIgnoreCase))
|
2020-03-22 14:29:12 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
value = value.Remove(value.LastIndexOf(forRemoving, StringComparison.InvariantCultureIgnoreCase));
|
2020-03-22 14:29:12 +01:00
|
|
|
}
|
2021-11-19 14:59:24 +01:00
|
|
|
return value;
|
|
|
|
|
}
|
2020-03-22 14:29:12 +01:00
|
|
|
|
|
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
public static string TrimStart(this string value, string forRemoving)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(value)) return value;
|
|
|
|
|
if (String.IsNullOrEmpty(forRemoving)) return value;
|
|
|
|
|
|
|
|
|
|
while (value.StartsWith(forRemoving, StringComparison.InvariantCultureIgnoreCase))
|
2020-03-22 14:29:12 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
value = value.Substring(forRemoving.Length);
|
2020-03-22 14:29:12 +01:00
|
|
|
}
|
2021-11-19 14:59:24 +01:00
|
|
|
return value;
|
|
|
|
|
}
|
2020-03-22 14:29:12 +01:00
|
|
|
|
|
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
public static bool HasValue(this string input)
|
|
|
|
|
{
|
|
|
|
|
return !String.IsNullOrWhiteSpace(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static bool IsNullOrEmpty(this string input)
|
|
|
|
|
{
|
|
|
|
|
return String.IsNullOrEmpty(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static bool IsNullOrWhiteSpace(this string input)
|
|
|
|
|
{
|
|
|
|
|
return String.IsNullOrWhiteSpace(input);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ToCamelCase(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrEmpty(input) && input.Length > 1)
|
2020-03-22 14:29:12 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
return Char.ToLowerInvariant(input[0]) + input.Substring(1);
|
2020-03-22 14:29:12 +01:00
|
|
|
}
|
2021-11-19 14:59:24 +01:00
|
|
|
return input;
|
|
|
|
|
}
|
2020-03-22 14:29:12 +01:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
public static string ToPascalCase(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrEmpty(input) && input.Length > 1)
|
2020-03-22 14:29:12 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
return Char.ToUpperInvariant(input[0]) + input.Substring(1);
|
2020-03-22 14:29:12 +01:00
|
|
|
}
|
2021-11-19 14:59:24 +01:00
|
|
|
return input;
|
|
|
|
|
}
|
2020-04-30 15:35:09 +02:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
public static string ToCamelCaseId(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(input))
|
2020-04-30 15:35:09 +02:00
|
|
|
{
|
2020-05-01 14:07:09 +02:00
|
|
|
return input;
|
2020-04-30 15:35:09 +02:00
|
|
|
}
|
2020-05-02 12:20:22 +02:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
if (input.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
return input.ToLowerInvariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] parts = input.Split('.');
|
|
|
|
|
|
|
|
|
|
return String.Join(".", parts.Select(x => x.ToCamelCase()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string ToPascalCaseId(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(input))
|
2020-09-09 11:02:22 +02:00
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
if (input.Length < 2)
|
2020-05-02 12:20:22 +02:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
return input.ToUpperInvariant();
|
2020-05-02 12:20:22 +02:00
|
|
|
}
|
2020-09-09 11:02:22 +02:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
string[] parts = input.Split('.');
|
2020-09-09 11:02:22 +02:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
return String.Join(".", parts.Select(x => x.ToPascalCase()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string RemoveNewLines(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(input))
|
2020-09-09 11:02:22 +02:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
return input;
|
2020-09-09 11:02:22 +02:00
|
|
|
}
|
2021-01-17 16:29:05 +01:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
return newLineCharsRegex.Replace(input, String.Empty).Trim();
|
|
|
|
|
}
|
2021-01-17 16:29:05 +01:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
|
|
|
|
|
public static string Shorten(this string input, int maxLength)
|
|
|
|
|
{
|
|
|
|
|
if (maxLength < 4)
|
2021-01-17 16:29:05 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
throw new ArgumentOutOfRangeException("maxLength", "Has to be at least 4 characters long");
|
2021-01-17 16:29:05 +01:00
|
|
|
}
|
2021-02-08 13:58:33 +01:00
|
|
|
|
2021-11-19 14:59:24 +01:00
|
|
|
if (String.IsNullOrEmpty(input) || input.Length <= maxLength)
|
2021-02-08 13:58:33 +01:00
|
|
|
{
|
2021-11-19 14:59:24 +01:00
|
|
|
return input;
|
2021-02-08 13:58:33 +01:00
|
|
|
}
|
2021-11-19 14:59:24 +01:00
|
|
|
|
|
|
|
|
return input.Substring(0, maxLength - 3) + "...";
|
2020-03-22 14:18:34 +01:00
|
|
|
}
|
|
|
|
|
}
|