diff --git a/Source/Cogworks.Essentials/Constants/StringConstants.Separators.cs b/Source/Cogworks.Essentials/Constants/StringConstants.Separators.cs index 343db32..54f46bf 100644 --- a/Source/Cogworks.Essentials/Constants/StringConstants.Separators.cs +++ b/Source/Cogworks.Essentials/Constants/StringConstants.Separators.cs @@ -16,6 +16,7 @@ public static class Separators public const string Vertical = "|"; public const string Hyphen = "-"; public const string HtmlNewLine = "
"; + public const string QuestionMark = "?"; } } } \ No newline at end of file diff --git a/Source/Cogworks.Essentials/Extensions/StringExtensions.cs b/Source/Cogworks.Essentials/Extensions/StringExtensions.cs index a1b4cc6..4ed2493 100644 --- a/Source/Cogworks.Essentials/Extensions/StringExtensions.cs +++ b/Source/Cogworks.Essentials/Extensions/StringExtensions.cs @@ -15,7 +15,9 @@ public static bool HasValue(this string input) => !string.IsNullOrWhiteSpace(input); public static Uri ToUri(this string urlString) - => new Uri(urlString); + => urlString.HasValue() + ? new Uri(urlString) + : null; public static string ToCamelCase(this string original) { @@ -222,5 +224,51 @@ public static string RemoveNewLines(this string input) .Replace("\r", string.Empty) .Replace(Environment.NewLine, string.Empty) : string.Empty; + + public static string GetIpAddressWithoutPort(this string ipAddress) + { + var portIndex = ipAddress.IndexOf(':'); + + return portIndex < 0 + ? ipAddress + : ipAddress.Substring(0, portIndex); + } + + public static string AddOrUpdateQueryParameter(this string url, string queryKey, string queryValue) + { + var splitted = url.Split(Separators.QuestionMark.ToCharArray()); + var queryString = HttpUtility.ParseQueryString(splitted.Skip(1).FirstOrDefault() ?? string.Empty); + + queryString[queryKey] = queryValue; + + return $"{splitted.FirstOrDefault()}?{queryString}"; + } + + /// + /// Strips out

and

tags if they were used as a wrapper + /// for other HTML content. + ///
+ /// The HTML text. + public static string RemoveParagraphWrapperTags(this string text) + { + if (!text.HasValue()) + { + return text; + } + + var trimmedText = text.Trim(); + var paragraphIndex = trimmedText.IndexOf("

", StringComparison.Ordinal); + + if (paragraphIndex != 0 + || paragraphIndex != trimmedText.LastIndexOf("

", StringComparison.Ordinal) + || trimmedText.Substring(trimmedText.Length - 4, 4) != "

") + { + // Paragraph not used as a wrapper element + return text; + } + + // Remove paragraph wrapper tags + return trimmedText.Substring(3, trimmedText.Length - 7); + } } } \ No newline at end of file