12345678910111213141516171819202122232425262728293031 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Utils
- {
- public static class ExtensionMethods
- {
- public static TR MapValue<TK, TV, TR>(this Dictionary<TK, TV> dic, TK key, Func<TV, TR> found)
- {
- return dic.TryGetValue(key, out var value)
- ? found(value)
- : default;
- }
- public static TR MapValue<TK, TV, TR>(this Dictionary<TK, TV> dic, TK key, Func<TV, TR> found, Func<TR> notFound)
- {
- return dic.TryGetValue(key, out var value)
- ? found(value)
- : notFound();
- }
- public static void FilterChars(this StringBuilder sb, HashSet<char> chars, char replace = '_')
- {
- for (var i = 0; i < sb.Length; i++)
- {
- if (chars.Contains(sb[i])) sb[i] = replace;
- }
- }
- }
- }
|