Files
BeWoPlaner/Shared/Extensions/StringExtensions.cs

114 lines
3.2 KiB
C#
Raw Normal View History

2016-06-27 01:45:38 +02:00
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;
}
2024-08-14 15:20:37 +02:00
public static bool IsNotNullOrEmpty(this string pString)
=> !IsNullOrEmpty(pString);
2016-06-27 01:45:38 +02:00
public static bool IsNullOrEmpty(this string pString)
{
return String.IsNullOrEmpty(pString);
}
public static List<string> Split(this string pString, string pSeparator)
{
if(pString == null)
return new List<string>();
return 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");
}
public static string RemoveUppercaseEsszett(this string pString)
{
return pString?.Replace("ẞ", "ß");
}
2024-11-22 16:30:43 +01:00
public static string ToNullIfEmpty(this string str)
{
return string.IsNullOrWhiteSpace(str) ? 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;
}
2016-06-27 01:45:38 +02:00
}
}