using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExtensionMethods
{
public static class StringExt
{
///
/// Zjistí počet znaků v řetězci
///
///
/// Znaky, které budou v řetězci vyhledány.
/// Počet znaků
public static int Count(this String s, params Char[] characters)
{
int Counter = 0;
foreach (char Character in s)
{
if (Array.IndexOf(characters, Character) != -1)
Counter++;
}
return Counter;
}
///
/// Metoda umožňuje zjistit počet znaků v řetězci, které odpovídají předanému predikátu
///
///
///
///
public static int Count(this String s, Predicate predicate)
{
int count = 0;
foreach (char Character in s)
if (predicate(Character))
count++;
return count;
}
///
/// Odstraní z řetězce diakritiku. Ostatní znaky jsou nezměněny.
///
///
/// Text bez diakritiky
public static string RemoveDiacritics(this String s)
{
// oddělení znaků od modifikátorů (háčků, čárek, atd.)
s = s.Normalize(System.Text.NormalizationForm.FormD);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < s.Length; i++)
{
// do řetězce přidá všechny znaky kromě modifikátorů
if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(s[i]) != System.Globalization.UnicodeCategory.NonSpacingMark)
{
sb.Append(s[i]);
}
}
// vrátí řetězec bez diakritiky
return sb.ToString();
}
///
/// Získá z řetězce jen ta písmena, která odpovídají předanému predikátu
///
/// Libovolný řetězec
/// Predikát, kterým bude testováno každé písmeno
/// Řetězec písmen, které odpovídají danému predikátu
public static string Filter(this String s, Predicate predicate)
{
StringBuilder sb = new StringBuilder();
foreach (char character in s)
{
if (predicate(character))
sb.Append(character.ToString());
}
return sb.ToString();
}
///
/// Obrátí řetězec
///
///
///
public static string Reverse(this string s)
{
return s.ToCharArray().Reverse().ToList().Implode("");
}
///
/// Odstraní z řetězce všechny výskyty daného znaku
///
///
///
///
public static string Delete(this string s, char c)
{
return s.Replace(c.ToString(), "");
}
public static string Remove(this string input, string old)
{
return input.Replace(old, string.Empty);
}
public static string Remove(this string input, string[] old)
{
string temp = input;
old.ForEach(s => temp = temp.Remove(s));
return temp;
}
public static string Replace(this string input, string[] oldStrings, string newString)
{
string temp = input;
oldStrings.ForEach(old => temp = temp.Replace(old, newString));
return temp;
}
public static string Replace(this string input, string[] oldString, string[] newString)
{
string temp = input;
for (int i = 0; i < oldString.Length; i++)
temp = temp.Replace(oldString[i], newString[i]);
return temp;
}
///
/// Vytvoří nový řetězec, ve kterém count-krát vloží za sebe key.
///
///
///
///
public static string Repeat(string key, int count)
{
StringBuilder sb = new StringBuilder();
count.Times(x => sb.Append(key));
return sb.ToString();
}
///
/// Vrátí prvních length znaků řetězce
///
///
///
///
public static string Take(this string s, int length)
{
return s.Substring(0, Math.Min(length, s.Length));
}
///
/// Náhodně přehází znaky v řetězci
///
///
///
public static string Shuffle(this string s)
{
return s.ToCharArray().Shuffle().Implode();
}
///
/// Vrací true, pokud řetězec obsahuje alespoň jeden znak v poli
///
///
///
///
public static bool ContainsSome(this string s, char[] chars)
{
foreach (char c in chars)
{
if (s.Contains(c))
return true;
}
return false;
}
///
/// Vrací true v případě, že řetězec obsahuje všechny znaky v poli
///
///
///
///
public static bool ContainsAll(this string s, char[] chars)
{
foreach (char c in chars)
{
if (!s.Contains(c))
return false;
}
return true;
}
}
}