using System.Text.RegularExpressions; using Strata.SqlTools.SqlBreakdown.Utilities; namespace Strata.SqlTools.SqlBreakdown.Extensions; /// /// Extension methods for string manipulation commonly used in SQL operations. /// public static class StringExtensions { /// /// Trims the specified number of characters from the beginning and end of the string. /// For instance, if aTrimLength is 1 and the string is "(Test)", the return value would be "Test". /// /// The string to trim. /// The number of characters to trim from each end. /// The trimmed string. public static string TakeInner(this string aString, int aTrimLength) => aString.Substring(aTrimLength, aString.Length - aTrimLength - 1); /// /// Returns the string value (identity function). /// /// The string. /// The same string. public static string Value(this string aString) => aString; /// /// Determines whether the string is null. /// /// The string to check. /// true if the string is null; otherwise, false. public static bool IsNull(this string aString) => aString is null; /// /// Returns the leftmost characters from a string. /// /// The string. /// The number of characters to return. /// The leftmost characters. /// Thrown when aCharCount is negative. public static string Left(this string aString, int aCharCount) { if (aCharCount >= aString.Length) { return aString; } if (aCharCount < 0) { throw new ArgumentException("Character count cannot be negative.", nameof(aCharCount)); } if (aCharCount == 0) { return string.Empty; } return aString.Substring(0, aCharCount); } /// /// Returns the rightmost characters from a string. /// /// The string. /// The number of characters to return. /// The rightmost characters. /// Thrown when aCharCount is negative. public static string Right(this string aString, int aCharCount) { if (aCharCount >= aString.Length) { return aString; } if (aCharCount < 0) { throw new ArgumentException("Character count cannot be negative.", nameof(aCharCount)); } if (aCharCount == 0) { return string.Empty; } return aString.Substring(aString.Length - aCharCount, aCharCount); } /// /// Determines whether the string is null or empty. /// /// The string to check. /// true if the string is null or empty; otherwise, false. public static bool IsNullOrEmpty(this string aString) => string.IsNullOrEmpty(aString); /// /// Determines whether the string has a value (is not null or empty). /// /// The string to check. /// true if the string has a value; otherwise, false. public static bool HasValue(this string aString) => !string.IsNullOrEmpty(aString); /// /// Reverses the characters in a string. /// /// The string to reverse. /// The reversed string. public static string Reverse(this string aString) { var arr = aString.ToArray(); Array.Reverse(arr); return new string(arr); } /// /// Determines whether the string is a valid GUID format. /// /// The string to check. /// true if the string is a valid GUID; otherwise, false. public static bool IsGUID(this string aString) { const string pattern = "^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$"; var match = Regex.Match(aString, pattern, RegexOptions.None, RegexDefaults.MatchTimeout); return match.Success; } /// /// Converts a string to a GUID. If it cannot be converted to a GUID, returns the default value. /// /// The string to convert. /// The default GUID to return if conversion fails. /// The GUID or the default value. public static Guid ToGUID(this string aString, Guid aDefault) => aString.IsGUID() ? new Guid(aString) : aDefault; /// /// Gets a string that is valid to be used as a string value in SQL by escaping single quotes. /// /// The string to make SQL-safe. /// The SQL-safe string with single quotes escaped. public static string GetSQLSafeString(this string aString) => aString.Replace("'", "''"); }