144 lines
5.3 KiB
C#
144 lines
5.3 KiB
C#
using System.Text.RegularExpressions;
|
|
using Strata.SqlTools.SqlBreakdown.Utilities;
|
|
|
|
namespace Strata.SqlTools.SqlBreakdown.Extensions;
|
|
|
|
/// <summary>
|
|
/// Extension methods for string manipulation commonly used in SQL operations.
|
|
/// </summary>
|
|
public static class StringExtensions
|
|
{
|
|
/// <summary>
|
|
/// 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".
|
|
/// </summary>
|
|
/// <param name="aString">The string to trim.</param>
|
|
/// <param name="aTrimLength">The number of characters to trim from each end.</param>
|
|
/// <returns>The trimmed string.</returns>
|
|
public static string TakeInner(this string aString, int aTrimLength) =>
|
|
aString.Substring(aTrimLength, aString.Length - aTrimLength - 1);
|
|
|
|
/// <summary>
|
|
/// Returns the string value (identity function).
|
|
/// </summary>
|
|
/// <param name="aString">The string.</param>
|
|
/// <returns>The same string.</returns>
|
|
public static string Value(this string aString) => aString;
|
|
|
|
/// <summary>
|
|
/// Determines whether the string is null.
|
|
/// </summary>
|
|
/// <param name="aString">The string to check.</param>
|
|
/// <returns>true if the string is null; otherwise, false.</returns>
|
|
public static bool IsNull(this string aString) => aString is null;
|
|
|
|
/// <summary>
|
|
/// Returns the leftmost characters from a string.
|
|
/// </summary>
|
|
/// <param name="aString">The string.</param>
|
|
/// <param name="aCharCount">The number of characters to return.</param>
|
|
/// <returns>The leftmost characters.</returns>
|
|
/// <exception cref="ArgumentException">Thrown when aCharCount is negative.</exception>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the rightmost characters from a string.
|
|
/// </summary>
|
|
/// <param name="aString">The string.</param>
|
|
/// <param name="aCharCount">The number of characters to return.</param>
|
|
/// <returns>The rightmost characters.</returns>
|
|
/// <exception cref="ArgumentException">Thrown when aCharCount is negative.</exception>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the string is null or empty.
|
|
/// </summary>
|
|
/// <param name="aString">The string to check.</param>
|
|
/// <returns>true if the string is null or empty; otherwise, false.</returns>
|
|
public static bool IsNullOrEmpty(this string aString) => string.IsNullOrEmpty(aString);
|
|
|
|
/// <summary>
|
|
/// Determines whether the string has a value (is not null or empty).
|
|
/// </summary>
|
|
/// <param name="aString">The string to check.</param>
|
|
/// <returns>true if the string has a value; otherwise, false.</returns>
|
|
public static bool HasValue(this string aString) => !string.IsNullOrEmpty(aString);
|
|
|
|
/// <summary>
|
|
/// Reverses the characters in a string.
|
|
/// </summary>
|
|
/// <param name="aString">The string to reverse.</param>
|
|
/// <returns>The reversed string.</returns>
|
|
public static string Reverse(this string aString)
|
|
{
|
|
var arr = aString.ToArray();
|
|
Array.Reverse(arr);
|
|
return new string(arr);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the string is a valid GUID format.
|
|
/// </summary>
|
|
/// <param name="aString">The string to check.</param>
|
|
/// <returns>true if the string is a valid GUID; otherwise, false.</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a string to a GUID. If it cannot be converted to a GUID, returns the default value.
|
|
/// </summary>
|
|
/// <param name="aString">The string to convert.</param>
|
|
/// <param name="aDefault">The default GUID to return if conversion fails.</param>
|
|
/// <returns>The GUID or the default value.</returns>
|
|
public static Guid ToGUID(this string aString, Guid aDefault) =>
|
|
aString.IsGUID() ? new Guid(aString) : aDefault;
|
|
|
|
/// <summary>
|
|
/// Gets a string that is valid to be used as a string value in SQL by escaping single quotes.
|
|
/// </summary>
|
|
/// <param name="aString">The string to make SQL-safe.</param>
|
|
/// <returns>The SQL-safe string with single quotes escaped.</returns>
|
|
public static string GetSQLSafeString(this string aString) => aString.Replace("'", "''");
|
|
}
|
|
|