Files
adas-core/adas-core.Domain/Utils/StringEx.cs
T

65 lines
3.5 KiB
C#

namespace adas_core.Domain.Utils;
/// <summary>
/// Provides static extension methods for the <see cref="string"/> type.
/// </summary>
/// <!-- aidoc:v1 sig=cc11c0a -->
public static class StringEx
{
/// <summary>
/// Determines whether the specified string is <c>null</c> or an empty string.
/// </summary>
/// <param name="source">The string to evaluate.</param>
/// <returns><c>true</c> if <paramref name="source"/> is <c>null</c> or an empty string; otherwise, <c>false</c>.</returns>
/// <!-- aidoc:v1 sig=6fb921b body=7fbbf9d -->
public static bool IsEmpty(this string source)
{
return string.IsNullOrEmpty(source);
}
/// <summary>
/// Determines whether the specified string is null, empty, or consists only of white-space characters.
/// </summary>
/// <param name="source">The string to evaluate.</param>
/// <returns>true if the string is null, empty, or contains only white space; otherwise, false.</returns>
/// <!-- aidoc:v1 sig=cec22b6 body=67e9275 -->
public static bool IsEmptyOrWhiteSpace(this string source)
{
return string.IsNullOrWhiteSpace(source);
}
/// <summary>
/// Returns the portion of the source string that follows the first occurrence of the specified value, using ordinal (case-sensitive, culture-insensitive) comparison. If the source or value is null or empty, or the value is not found within the source, the original source is returned unchanged.
/// </summary>
/// <param name="source">The string to search within.</param>
/// <param name="value">The delimiter whose first occurrence marks the start of the returned substring.</param>
/// <returns>The substring after the first occurrence of <paramref name="value"/>; otherwise, the original <paramref name="source"/>.</returns>
/// <!-- aidoc:v1 sig=e1718c5 body=92784ed -->
public static string SubstringAfter(this string source, string value)
{
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(value)) return source;
var index = source.IndexOf(value, StringComparison.Ordinal);
return index >= 0
? source.Substring(index + value.Length)
: source;
}
/// <summary>
/// Returns the portion of <paramref name="source"/> that precedes the first occurrence of <paramref name="value"/>, using ordinal comparison.
/// If either <paramref name="source"/> or <paramref name="value"/> is null or empty, or if <paramref name="value"/> is not found within <paramref name="source"/>, the original <paramref name="source"/> is returned unchanged.
/// </summary>
/// <param name="source">The string to extract the substring from.</param>
/// <param name="value">The delimiter whose first occurrence marks the end of the returned substring.</param>
/// <returns>The substring of <paramref name="source"/> before the first occurrence of <paramref name="value"/>, or the original <paramref name="source"/> when no match is found or when either input is null or empty.</returns>
/// <!-- aidoc:v1 sig=a2fe204 body=2083b5c -->
public static string SubstringBefore(this string source, string value)
{
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(value)) return source;
var index = source.IndexOf(value, StringComparison.Ordinal);
return index >= 0
? source.Substring(0, index)
: source;
}
}