using System;
namespace ExtensionMethods
{
///
/// Add extensions to
///
public static class StringExtensions
{
///
/// Determines whether the specified is null or an string
///
///
/// . True: String is null or empty. False: String is not null and not empty
public static bool IsEmpty(this string str)
{
return String.IsNullOrEmpty(str);
}
///
/// Determines whether the specified is null or an string or does match the specified string
///
///
/// When the matches this it is also considered as "empty"
/// . True: String is null or empty. False: String is not null and not empty
public static bool IsEmptyOr(this string str, string alsoEmpty)
{
return String.IsNullOrEmpty(str) || str == alsoEmpty;
}
///
/// Determines whether the specified is null or an string or does match the specified strings or
///
///
/// When the matches this it is also considered as "empty"
/// When the matches this it is also considered as "empty"
/// . True: String is null or empty. False: String is not null and not empty
public static bool IsEmptyOr(this string str, string alsoEmpty1, string alsoEmpty2)
{
return String.IsNullOrEmpty(str) || str == alsoEmpty1 || str == alsoEmpty2;
}
///
/// Determines whether the specified is null or an string or any of the specified strings (
///
///
/// When the matches this it is also considered as "empty"
/// . True: String is null or empty. False: String is not null and not empty
public static bool IsEmptyOr(this string str, params string[] alsoEmpty)
{
if (String.IsNullOrEmpty(str))
return true;
foreach (string also in alsoEmpty)
if (str == also)
return true;
return false;
}
///
/// Determines whether the specified is not null and not an string
///
///
/// . True: String is not null and not empty. False: String is null or empty
public static bool IsNotEmpty(this string str)
{
return !String.IsNullOrEmpty(str);
}
///
/// Determines whether the specified is not null, not an string and does not match the specified string
///
///
/// When the matches this it is also considered as "empty"
/// . True: String is not null, not empty and not equal to . False: String is null or empty
public static bool IsNeitherEmptyNor(this string str, string alsoEmpty)
{
return !String.IsNullOrEmpty(str) && str != alsoEmpty;
}
///
/// Determines whether the specified is not null, not an string and does not match the specified strings or
///
///
/// When the matches this this function also returns false
/// When the matches this this function also returns false
/// . True: String is not null, not empty and not equal to or .
public static bool IsNeitherEmptyNor(this string str, string alsoEmpty1, string alsoEmpty2)
{
return !String.IsNullOrEmpty(str) && str != alsoEmpty1 && str != alsoEmpty2;
}
///
/// Determines whether the specified is not null, not an string and does not match any of the specified strings
///
///
/// When the matches one of these s this function returns false
/// ean. True: String is not null, not empty and not equal to .
public static bool IsNeitherEmptyNor(this string str, params string[] alsoEmpty)
{
if (String.IsNullOrEmpty(str))
return false;
foreach (string also in alsoEmpty)
if (str == also)
return false;
return true;
}
///
/// Join this array with the delimiter + and begin and end at the beginning and at the end respectively
/// Example: Begin: "B", End: "E-" --> B1E-B2E-B3E-
///
/// Example: Begin: "B", End: "E-" --> B1E-B2E-B3E-
///
///
///
///
public static string Join(this string[] strs, string begin, string end)
{
string ret = begin;
ret += String.Join(end + begin, strs);
ret += end;
return ret;
}
///
/// Join this array with the delimiter + and begin and end at the beginning and at the end respectively
/// Example: Begin: "B", End: "E-" --> B1E-B2E-B3E
///
/// Example: Begin: "B", Delimiter "-", End: "E" --> B1E-B2E-B3E
///
///
///
///
///
public static string Join(this string[] strs, string begin, string delimiter, string end)
{
string ret = begin;
ret += String.Join(end + delimiter + begin, strs);
ret += end;
return ret;
}
///
/// Concatenates a specified separator between each element of a specified array, yielding a single concatenated string
///
///
/// between the array elements
/// A single concatenated string
public static string Join(this string[] strs, string delimiter)
{
return String.Join(delimiter, strs);
}
///
/// Retrieves a substring from this instance. The substring starts at a specified character position and has a specified .
/// If the length of the instance is shorter than the specified the ing itself is returned
///
///
/// Character position where substring shall start, beginning at 0
/// The (maximum) length of the substring
/// Substring
public static string SafeSubstring(this string str, int startIndex, int length)
{
if (str.Length <= length)
return str;
else if (startIndex + length <= str.Length)
return str.Substring(startIndex, length);
else
return str.Substring(startIndex, str.Length - startIndex);
}
///
/// Returns the value of this or .Value if the string or matches
///
///
/// When the matches this it is also considered as "empty"
/// The or
public static object DBValue(this string str, string considerEmpty)
{
if (str.IsEmptyOr(considerEmpty))
return DBNull.Value;
else
return str;
}
}
}