Files
BeWoPlaner/Shared/Extensions/StringExtensions.cs
Lyndon 8ae75cfc77 Personenmodul hinzugefügt - schreibgeschützt
Mitarbeitermodul mit Abwesenheiten hinzugefügt
Einbau des Dokumententeils vereinfacht.
2026-02-04 18:03:01 +01:00

147 lines
4.5 KiB
C#

using BS.Shared.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Xml;
namespace BS.Shared.Extensions
{
public static class StringExtensions
{
public static bool ContainsAll(this string pString, IEnumerable<string> pToCheck)
{
string lLow = pString.ToLower();
foreach (var iS in pToCheck)
{
if (!lLow.Contains(iS.Trim().ToLower()))
{
return false;
}
}
return true;
}
public static bool IsNotNullOrEmpty(this string pString)
=> !IsNullOrEmpty(pString);
public static bool IsNullOrEmpty(this string pString)
{
return String.IsNullOrEmpty(pString);
}
public static List<string> Split(this string pString, string pSeparator)
{
return pString is null ? new List<string>() : pString.Split(new[] { pSeparator }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
public static IEnumerable<KeyValuePair<string, string>> SplitToKeyValuePairList(this string pString, string pSeparator1, string pSeperator2)
{
if(pString == null)
return new KeyValuePair<string, string>[0];
var preResult = pString.Split(pSeparator1);
return preResult.Select(pair => new KeyValuePair<string, string>(pair.Split(pSeperator2)[0], pair.Split(pSeperator2)[1])).ToList();
}
public static string GetRecurrenceInfoAttribute(this string recurrenceInfo, string attribute)
{
using (var reader = XmlReader.Create(new StringReader(recurrenceInfo)))
{
reader.ReadToFollowing("RecurrenceInfo");
reader.MoveToAttribute(attribute);
return reader.Value;
}
}
public static T GetValueFromDescription<T>(this string description)
{
var type = typeof(T);
if (!type.IsEnum) throw new InvalidOperationException();
foreach (var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
return (T)field.GetValue(null);
}
else
{
if (field.Name == description)
return (T)field.GetValue(null);
}
}
throw new ArgumentException("Not found.", "description");
}
/// <summary>
/// Ersetzt das große ẞ durch ein kleines, da das große ẞ nicht von latin01 inder Datenbank unterstützt wird.
/// </summary>
/// <param name="pString">String, der möglicherweise ein großes ẞ enthält.</param>
/// <returns>pString, mit ersetzten, großen ẞ, falls vorher vorhanden.</returns>
public static string RemoveUppercaseEsszett(this string pString)
{
return pString?.Replace("ẞ", "ß");
}
public static bool IsNotNullOrWhiteSpace(this string pString)
=> !pString.IsNullOrWhiteSpace();
public static bool IsNullOrWhiteSpace(this string str)
=> string.IsNullOrEmpty(str);
public static string ToNullIfEmpty(this string str)
=> str.IsNullOrWhiteSpace() ? null : str;
public static string ToFormatFollows(this string str1, string str2, char seperator = ' ')
{
str1 = str1.ToNullIfEmpty();
str2 = str2.ToNullIfEmpty();
var rtn = str1;
if(str2 is string)
{
if (rtn is string)
rtn += seperator;
rtn += str2;
}
return rtn;
}
public static bool AreAllTheSame(this IEnumerable<string> list)
{
if (list is null || !list.Any())
return true;
string init = list.First();
foreach (var str in list)
{
if (str != init)
return false;
}
return true;
}
public static string SenderAddressToString(this IInvoiceBase invoiceBase)
=> $"{invoiceBase.SenderStreet}, {invoiceBase.SenderTown}, {invoiceBase.SenderPostCode}";
public static string FirstCharToUpper(this string input)
{
if(string.IsNullOrWhiteSpace(input))
{
return input;
}
return char.ToUpper(input[0]) + input.Substring(1);
}
}
}