65 lines
1.2 KiB
C#
65 lines
1.2 KiB
C#
namespace MyOffice.Core.Extensions;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
public static class StringExtensions
|
|
{
|
|
public static bool IsMissing(this string? str)
|
|
{
|
|
return str == null || string.IsNullOrEmpty(str) || string.IsNullOrEmpty(str.Trim());
|
|
}
|
|
|
|
public static bool IsPresent(this string? str)
|
|
{
|
|
return !IsMissing(str);
|
|
}
|
|
|
|
public static string? NullIfEmpty(this string? str)
|
|
{
|
|
return IsMissing(str) ? null : str;
|
|
}
|
|
|
|
public static bool EqualsIgnoreCase(this string? str, string? value)
|
|
{
|
|
return str != null && str.Equals(value, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
public static string? SafeTrim(this string? str)
|
|
{
|
|
return str == null ? str : str!.Trim();
|
|
}
|
|
|
|
public static string? MaxOf(this string? str, int maxLength)
|
|
{
|
|
if (str == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (str.Length < maxLength)
|
|
{
|
|
return str;
|
|
}
|
|
|
|
return str.Substring(0, maxLength);
|
|
}
|
|
|
|
public static bool AsBool(this string? str, bool defaultValue = false)
|
|
{
|
|
return true.ToString().Equals(str, StringComparison.OrdinalIgnoreCase) || defaultValue;
|
|
}
|
|
|
|
public static Guid AsGuid(this string str)
|
|
{
|
|
return Guid.Parse(str);
|
|
}
|
|
|
|
public static Guid? AsGuidNull(this string str)
|
|
{
|
|
if (Guid.TryParse(str, out var guid))
|
|
{
|
|
return guid;
|
|
}
|
|
return null;
|
|
}
|
|
} |