Show / Hide Table of Contents

Class StringExtensionMethods

Extension methods for string.

Inheritance
object
StringExtensionMethods
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: Benday.Common
Assembly: Benday.Common.dll
Syntax
public static class StringExtensionMethods

Methods

| Edit this page View Source

EqualsCaseInsensitive(string?, string?)

This method compares two strings for equality, ignoring case. If both strings are null, they are considered equal.

Declaration
public static bool EqualsCaseInsensitive(this string? value1, string? value2)
Parameters
Type Name Description
string value1
string value2
Returns
Type Description
bool
| Edit this page View Source

IsNullOrWhitespace(string?)

This method checks if the string is null or empty. If the string is null or empty, it returns true.

Declaration
public static bool IsNullOrWhitespace(this string? value)
Parameters
Type Name Description
string value
Returns
Type Description
bool
| Edit this page View Source

SafeContains(string?, string)

This method checks if the string contains a value. If the string is null or empty, it returns false.

Declaration
public static bool SafeContains(this string? value, string containsValue)
Parameters
Type Name Description
string value
string containsValue
Returns
Type Description
bool
| Edit this page View Source

SafeContainsCaseInsensitive(string?, string)

This method checks if the string contains a value ignoring case. If the string is null or empty, it returns false.

Declaration
public static bool SafeContainsCaseInsensitive(this string? value, string containsValue)
Parameters
Type Name Description
string value
string containsValue
Returns
Type Description
bool
| Edit this page View Source

SafeToInt32(string?, int)

This method converts a string to an integer. If the string is null or empty, it returns the default value. If the string cannot be converted to an integer, it returns the default value.

Declaration
public static int SafeToInt32(this string? value, int defaultValue = 0)
Parameters
Type Name Description
string value
int defaultValue
Returns
Type Description
int
| Edit this page View Source

SafeToString(string?, string)

This wraps the ToString() method and handles the case where the string is null. Returns the input string if it is not null, otherwise returns the default value.

Declaration
public static string SafeToString(this string? input, string defaultValue = "")
Parameters
Type Name Description
string input
string defaultValue

Value to return if null. The default value is empty string.

Returns
Type Description
string
| Edit this page View Source

ToStringThrowIfNullOrEmpty(string?, string)

This method converts a string to a string and throws an exception if the string is null or empty.

Declaration
public static string ToStringThrowIfNullOrEmpty(this string? input, string label = "")
Parameters
Type Name Description
string input

Input string to check

string label

Optional label/name for the string to include in the exception

Returns
Type Description
string
Exceptions
Type Condition
InvalidOperationException
  • Edit this page
  • View Source
In this article
Back to top Copyright © www.benday.com | info@benday.com