StringExtensions Class
The class provides extension methods for enhancing the functionality and ease of use of strings.
public static class StringExtensionsInheritance System.Object → StringExtensions
Methods
StringExtensions.HasValue(this string) Method
Determines whether the specified string is neither null nor empty.
public static bool HasValue(this string? value);Parameters
value System.String
The string to test.
Returns
System.Boolean
true if the value parameter is not null or an empty string (""); otherwise, false.
StringExtensions.InsertAfter(this string, string, string) Method
Inserts a specified value into the source string after a specified fragment.
public static string InsertAfter(this string source, string fragment, string value);Parameters
source System.String
The source string where the value will be inserted.
fragment System.String
The fragment after which the value will be inserted.
value System.String
The value to insert into the source string.
Returns
System.String
A new string with the value inserted.
Exceptions
System.InvalidOperationException
Thrown when the fragment is not found in the source string.
StringExtensions.NotNullOrEmpty(this string, string) Method
Ensures that a string is neither null nor empty, throwing an exception if it is.
public static string NotNullOrEmpty(this string? value, string valueName);Parameters
value System.String
The string to validate.
valueName System.String
The name of the string variable, used in the exception message.
Returns
System.String
The original string if it is neither null nor empty.
Exceptions
System.InvalidOperationException
Thrown if the string is null or empty.
StringExtensions.NotNullOrWhiteSpace(this string) Method
Determines whether the specified string is not null, empty, or consists only of white-space characters.
public static bool NotNullOrWhiteSpace(this string? value);Parameters
value System.String
The string to test.
Returns
System.Boolean
true if the value parameter is not null, empty, or whitespace-only; otherwise, false.
StringExtensions.TrimSuffixIfExists(this string, string) Method
Trims the specified suffix from the end of the string, if it exists.
public static string TrimSuffixIfExists(this string source, string suffix);Parameters
source System.String
The source string to trim.
suffix System.String
The suffix to remove if it exists at the end of the source string.
Returns
System.String
The string without the specified suffix.