From 370cadbaeb21203beedf5563505fcefd424373f9 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 23 Mar 2021 17:05:27 -0400 Subject: [PATCH] feat: Extension methods for joining enumerables of strings --- .../Extensions/StringExtensions.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ICD.Common.Utils/Extensions/StringExtensions.cs b/ICD.Common.Utils/Extensions/StringExtensions.cs index b82d1d7..1bcf158 100644 --- a/ICD.Common.Utils/Extensions/StringExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Text.RegularExpressions; using ICD.Common.Properties; @@ -142,6 +143,46 @@ namespace ICD.Common.Utils.Extensions Math.Min(chunkSize, extends.Length - (i * chunkSize)))); } + /// + /// Joins the strings in the enumerable. + /// + /// + /// + public static string Join([NotNull] this IEnumerable extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.Join(string.Empty); + } + + /// + /// Joins the strings in the enumerable. + /// + /// + /// + /// + public static string Join([NotNull] this IEnumerable extends, string delimiter) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + StringBuilder builder = new StringBuilder(); + bool isFirst = true; + + foreach (string item in extends) + { + if (!isFirst) + builder.Append(delimiter); + + builder.Append(item); + + isFirst = false; + } + + return builder.ToString(); + } + /// /// Removes whitespace from the string. ///