From bf8c320d66f85020a1067f1cd10903f9156bea33 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Mon, 31 Aug 2020 13:37:38 -0400 Subject: [PATCH] feat: Added util methods for removing duplicate whitespace --- ICD.Common.Utils/Extensions/StringExtensions.cs | 15 +++++++++++++++ ICD.Common.Utils/StringUtils.cs | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/ICD.Common.Utils/Extensions/StringExtensions.cs b/ICD.Common.Utils/Extensions/StringExtensions.cs index 0e263f3..b82d1d7 100644 --- a/ICD.Common.Utils/Extensions/StringExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringExtensions.cs @@ -157,6 +157,21 @@ namespace ICD.Common.Utils.Extensions return new string(extends.Where(c => !char.IsWhiteSpace(c)).ToArray()); } + /// + /// Replaces spans of whitespace with a single space. + /// + /// + /// + [PublicAPI] + [NotNull] + public static string RemoveDuplicateWhitespace([NotNull] this string extends) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return Regex.Replace(extends, @"\s+", " "); + } + /// /// Removes all occurrences of the given string. /// diff --git a/ICD.Common.Utils/StringUtils.cs b/ICD.Common.Utils/StringUtils.cs index 43d7533..6ffd051 100644 --- a/ICD.Common.Utils/StringUtils.cs +++ b/ICD.Common.Utils/StringUtils.cs @@ -647,6 +647,16 @@ namespace ICD.Common.Utils return text == null ? null : text.RemoveWhitespace(); } + /// + /// Replaces spans of whitespace with a single space. + /// + /// + /// + public static string RemoveDuplicateWhitespace(string text) + { + return text == null ? null : text.RemoveDuplicateWhitespace(); + } + /// /// Returns true if the string is entirely whitespace characters, or empty, or null. ///