Files
SabreTools.IO/SabreTools.Text.Extensions/StringExtensions.cs

68 lines
2.3 KiB
C#
Raw Permalink Normal View History

2025-09-06 15:32:36 -04:00
using System;
namespace SabreTools.Text.Extensions
2025-09-06 15:32:36 -04:00
{
public static class StringExtensions
{
2026-03-18 17:25:08 -04:00
#region Optional Extensions
2025-09-06 15:32:36 -04:00
/// <inheritdoc cref="string.Contains(string)"/>
public static bool OptionalContains(this string? self, string value)
=> OptionalContains(self, value, StringComparison.Ordinal);
/// <inheritdoc cref="string.Contains(string, StringComparison)"/>
public static bool OptionalContains(this string? self, string value, StringComparison comparisonType)
{
2026-01-25 17:04:11 -05:00
if (self is null)
2025-09-06 15:32:36 -04:00
return false;
#if NETFRAMEWORK || NETSTANDARD2_0
2025-09-06 15:32:36 -04:00
return self.Contains(value);
#else
return self.Contains(value, comparisonType);
#endif
}
2025-09-20 18:04:37 -04:00
/// <inheritdoc cref="string.EndsWith(string)"/>
public static bool OptionalEndsWith(this string? self, string value)
=> OptionalEndsWith(self, value, StringComparison.Ordinal);
/// <inheritdoc cref="string.EndsWith(string, StringComparison)"/>
public static bool OptionalEndsWith(this string? self, string value, StringComparison comparisonType)
{
2026-01-25 17:04:11 -05:00
if (self is null)
2025-09-20 18:04:37 -04:00
return false;
return self.EndsWith(value, comparisonType);
}
2025-09-06 15:32:36 -04:00
/// <inheritdoc cref="string.Equals(string)"/>
public static bool OptionalEquals(this string? self, string value)
=> OptionalEquals(self, value, StringComparison.Ordinal);
/// <inheritdoc cref="string.Equals(string, StringComparison)"/>
public static bool OptionalEquals(this string? self, string value, StringComparison comparisonType)
{
2026-01-25 17:04:11 -05:00
if (self is null)
2025-09-06 15:32:36 -04:00
return false;
return self.Equals(value, comparisonType);
}
/// <inheritdoc cref="string.StartsWith(string)"/>
public static bool OptionalStartsWith(this string? self, string value)
=> OptionalStartsWith(self, value, StringComparison.Ordinal);
/// <inheritdoc cref="string.StartsWith(string, StringComparison)"/>
public static bool OptionalStartsWith(this string? self, string value, StringComparison comparisonType)
{
2026-01-25 17:04:11 -05:00
if (self is null)
2025-09-06 15:32:36 -04:00
return false;
return self.StartsWith(value, comparisonType);
}
2026-03-18 17:25:08 -04:00
#endregion
2025-09-06 15:32:36 -04:00
}
2025-11-13 08:56:30 -05:00
}