2020-03-22 14:18:34 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
2020-05-02 12:20:22 +02:00
|
|
|
using System.Linq;
|
2020-03-22 14:18:34 +01:00
|
|
|
|
2020-03-24 23:09:29 +01:00
|
|
|
namespace zero.Core.Extensions
|
2020-03-22 14:18:34 +01:00
|
|
|
{
|
|
|
|
|
public static class StringExtensions
|
|
|
|
|
{
|
|
|
|
|
public static string EnsureStartsWith(this string input, string toStartWith)
|
|
|
|
|
{
|
|
|
|
|
if (input.StartsWith(toStartWith)) return input;
|
|
|
|
|
return toStartWith + input.TrimStart(toStartWith);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 14:29:12 +01:00
|
|
|
|
2020-03-22 14:18:34 +01:00
|
|
|
public static string EnsureStartsWith(this string input, char value)
|
|
|
|
|
{
|
|
|
|
|
return input.StartsWith(value.ToString(CultureInfo.InvariantCulture)) ? input : value + input;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 14:29:12 +01:00
|
|
|
|
2020-03-22 14:18:34 +01:00
|
|
|
public static string EnsureEndsWith(this string input, char value)
|
|
|
|
|
{
|
|
|
|
|
return input.EndsWith(value.ToString(CultureInfo.InvariantCulture)) ? input : input + value;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 14:29:12 +01:00
|
|
|
|
2020-03-22 14:18:34 +01:00
|
|
|
public static string EnsureEndsWith(this string input, string toEndWith)
|
|
|
|
|
{
|
|
|
|
|
return input.EndsWith(toEndWith.ToString(CultureInfo.InvariantCulture)) ? input : input + toEndWith;
|
|
|
|
|
}
|
2020-03-22 14:29:12 +01:00
|
|
|
|
|
|
|
|
|
2020-04-15 14:09:52 +02:00
|
|
|
public static string EnsureSurroundedWith(this string input, char toSurroundWith)
|
|
|
|
|
{
|
|
|
|
|
return input.EnsureStartsWith(toSurroundWith).EnsureEndsWith(toSurroundWith);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-13 13:50:25 +01:00
|
|
|
public static string Or(this string value, string fallback)
|
|
|
|
|
{
|
|
|
|
|
return String.IsNullOrWhiteSpace(value) ? fallback : value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-03-22 14:29:12 +01:00
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
value = value.Remove(value.LastIndexOf(forRemoving, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
value = value.Substring(forRemoving.Length);
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-04-30 15:35:09 +02:00
|
|
|
|
|
|
|
|
public static string ToCamelCase(this string input)
|
|
|
|
|
{
|
2020-05-01 14:07:09 +02:00
|
|
|
if (!String.IsNullOrEmpty(input) && input.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
return Char.ToLowerInvariant(input[0]) + input.Substring(1);
|
|
|
|
|
}
|
|
|
|
|
return input;
|
2020-04-30 15:35:09 +02:00
|
|
|
}
|
2020-05-02 12:20:22 +02:00
|
|
|
|
2020-09-09 11:02:22 +02:00
|
|
|
public static string ToPascalCase(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (!String.IsNullOrEmpty(input) && input.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
return Char.ToUpperInvariant(input[0]) + input.Substring(1);
|
|
|
|
|
}
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-02 12:20:22 +02:00
|
|
|
public static string ToCamelCaseId(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(input))
|
|
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
return input.ToLowerInvariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] parts = input.Split('.');
|
|
|
|
|
|
|
|
|
|
return String.Join(".", parts.Select(x => x.ToCamelCase()));
|
|
|
|
|
}
|
2020-09-09 11:02:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public static string ToPascalCaseId(this string input)
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(input))
|
|
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input.Length < 2)
|
|
|
|
|
{
|
|
|
|
|
return input.ToUpperInvariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] parts = input.Split('.');
|
|
|
|
|
|
|
|
|
|
return String.Join(".", parts.Select(x => x.ToPascalCase()));
|
|
|
|
|
}
|
2020-03-22 14:18:34 +01:00
|
|
|
}
|
|
|
|
|
}
|