StringExt.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. namespace qdr.app.studiou.orders2printpack.Extensions
  2. {
  3. public static class StringExt
  4. {
  5. public static string ToQuotedString(this string str)
  6. {
  7. if (str == null)
  8. return string.Empty;
  9. return ToDecoratedString(str, "\"");
  10. }
  11. public static string ToDecoratedString(this string str, string leftDecoration, string rightDecoration)
  12. {
  13. if (str == null)
  14. return string.Empty;
  15. return leftDecoration + str + rightDecoration;
  16. }
  17. public static string ToDecoratedString(this string str, string bothDecoration)
  18. {
  19. if (str == null)
  20. return string.Empty;
  21. return ToDecoratedString(str, bothDecoration, bothDecoration);
  22. }
  23. /// <summary>
  24. /// Returns a list of all duplicate string items in a collection.
  25. /// </summary>
  26. /// <param name="source">The source collection to check for duplicates.</param>
  27. /// <returns>An IEnumerable containing only the duplicate items.</returns>
  28. public static IEnumerable<string> GetDuplicates(this IEnumerable<string> source)
  29. {
  30. if (source == null)
  31. throw new ArgumentNullException(nameof(source));
  32. return source
  33. .GroupBy(x => x)
  34. .Where(g => g.Count() > 1)
  35. .Select(g => g.Key);
  36. }
  37. }
  38. }