92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
namespace Indotalent.ConfigBackEnd.Extensions;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
public static class StringExtensions
|
|
{
|
|
public static string ToShortNameVowel(this string? value, int length = 3)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return "GEN";
|
|
|
|
var vowels = new string(value.Where(c => "AEIOUaeiou".Contains(c)).ToArray());
|
|
var baseName = vowels.Length >= length ? vowels : value;
|
|
|
|
return new string(baseName.Trim().Take(length).ToArray()).ToUpper();
|
|
}
|
|
|
|
public static string ToShortNameConsonant(this string? value, int length = 3)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return "GEN";
|
|
|
|
var consonants = new string(value.Where(c => char.IsLetter(c) && !"AEIOUaeiou".Contains(c)).ToArray());
|
|
var baseName = consonants.Length >= length ? consonants : value;
|
|
|
|
return new string(baseName.Trim().Take(length).ToArray()).ToUpper();
|
|
}
|
|
|
|
public static string ToInitial(this string? value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return "XX";
|
|
|
|
var words = value.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
if (words.Length >= 2)
|
|
{
|
|
var firstInitial = words[0][0];
|
|
var secondInitial = words[1][0];
|
|
return $"{firstInitial}{secondInitial}".ToUpper();
|
|
}
|
|
|
|
return new string(value.Trim().Take(2).ToArray()).ToUpper();
|
|
}
|
|
|
|
public static bool IsNotNullOrWhiteSpace(this string? value)
|
|
{
|
|
return !string.IsNullOrWhiteSpace(value);
|
|
}
|
|
|
|
public static string ToTitleCase(this string? value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
|
|
|
|
return System.Globalization.CultureInfo.CurrentCulture.TextInfo
|
|
.ToTitleCase(value.ToLower().Trim());
|
|
}
|
|
|
|
public static string ToSlug(this string? value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
|
|
|
|
var str = value.ToLower().Trim();
|
|
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
|
|
str = Regex.Replace(str, @"[\s-]+", " ").Trim();
|
|
str = str.Replace(" ", "-");
|
|
|
|
return str;
|
|
}
|
|
|
|
public static string MaskEmail(this string? email)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(email) || !email.Contains("@")) return "******";
|
|
|
|
var parts = email.Split('@');
|
|
var name = parts[0];
|
|
var domain = parts[1];
|
|
|
|
if (name.Length <= 2) return $"{name}***@{domain}";
|
|
|
|
return $"{name[..2]}***{name[^1..]}@{domain}";
|
|
}
|
|
|
|
public static string Truncate(this string? value, int maxLength)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
|
|
return value.Length <= maxLength ? value : $"{value[..maxLength]}...";
|
|
}
|
|
|
|
public static string RemoveNonNumeric(this string? value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
|
|
return Regex.Replace(value, "[^0-9]", "");
|
|
}
|
|
} |