string extensions

String Extensions

Posted on Updated on

Here is my class of string extensions I like to use. It allows me to write code that is easier for me to read. For me, I find the following two examples a lot easier to read and allows me to be more consistent in writing code.

// Test if two string are not equal
string s1 = "this is string #1";
string s2 = "this is string #2";

if ( s1.IsNotEqualIgnoreCase( s2 ) )
    DoSomething();
// convert a string to an integer
string s = "12356";
int x = s.ToInt();

C# class Extensions:

using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Dahl.Core.Extensions
{
    public static class StringExt
    {
        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Trims whitespace from the string.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string TrimWhiteSpace( this string source )
        {
            if ( string.IsNullOrEmpty( source ) )
                return string.Empty;

            return source.Trim();
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Determines whether two T:string objects have the same value ignoring case,
        /// uses StringComparison.OrdinalIgnoreCase 
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="val">the value to compare</param>
        /// <returns></returns>
        public static bool IsEqualIgnoreCase( this string source, string val )
        {
            if ( string.IsNullOrEmpty( source ) && string.IsNullOrEmpty( val ) )
                return false;

            if ( string.IsNullOrEmpty( source ) )
                return false;

            return string.Equals( source, val, StringComparison.OrdinalIgnoreCase );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Determines whether 2 specified T:String objects have the same value ignoring case,
        /// uses StringComparison.OrdinalIgnoreCase 
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="val">the value to compare</param>
        /// <returns></returns>
        public static bool IsNotEqual( this string source, string val )
        {
            if ( string.IsNullOrEmpty( source ) && !string.IsNullOrEmpty( val ) )
                return true;

            if ( string.IsNullOrEmpty( val ) )
                return true;

            return !string.Equals( source, val );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Determines whether 2 specified T:String objects have the same value ignoring case,
        /// uses StringComparison.OrdinalIgnoreCase 
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="val">the value to compare</param>
        /// <returns></returns>
        public static bool IsNotEqualIgnoreCase( this string source, string val )
        {
            if ( string.IsNullOrEmpty( source ) && !string.IsNullOrEmpty( val ) )
                return true;

            if ( string.IsNullOrEmpty( val ) )
                return true;

            return !string.Equals( source, val, StringComparison.OrdinalIgnoreCase );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns a value indicating whether the specified String object occurs within this string ignoring case,
        /// uses StringComparison.OrdinalIgnoreCase 
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="val">the value to find</param>
        /// <returns></returns>
        public static bool ContainsIgnoreCase( this string source, string val )
        {
            if ( string.IsNullOrEmpty( source ) || string.IsNullOrEmpty( val ) )
                return false;

            return source.IndexOf( val, StringComparison.OrdinalIgnoreCase ) >= 0;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns a value indicating whether the specified String object contains anything
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <returns></returns>
        public static bool IsNullOrEmpty( this string source )
        {
            return string.IsNullOrEmpty( source );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns a value indicating whether the specified String object contains anything
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <returns></returns>
        public static bool IsNotNullOrEmpty( this string source )
        {
            return !string.IsNullOrEmpty( source );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Determines whether the beginning of this string instance matches the specified string ignoring case,
        /// uses StringComparison.OrdinalIgnoreCase 
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="val">the value to find</param>
        /// <returns></returns>
        public static bool StartsWithIgnoreCase( this string source, string val )
        {
            if ( string.IsNullOrEmpty( source ) || string.IsNullOrEmpty( val ) )
                return false;

            return source.StartsWith( val, StringComparison.OrdinalIgnoreCase );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Determines whether the end of this string instance matches the specified string ignoring case,
        /// uses StringComparison.OrdinalIgnoreCase 
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="val">the value to find</param>
        /// <returns></returns>
        public static bool EndsWithIgnoreCase( this string source, string val )
        {
            if ( string.IsNullOrEmpty( source ) || string.IsNullOrEmpty( val ) )
                return false;

            return source.EndsWith( val, StringComparison.OrdinalIgnoreCase );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Replaces the old value with the specified new value ignoring case.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="oldValue">The part of the string to be replaced by the newValue</param>
        /// <param name="newValue">The new string to replace the old value</param>
        /// <returns></returns>
        public static string ReplaceIgnoreCase( this string source, string oldValue, string newValue )
        {
            string s;
            try
            {
                if ( source == null )
                    source = string.Empty;

                if ( oldValue == null )
                    oldValue = string.Empty;

                s = Regex.Replace( source, oldValue, newValue, RegexOptions.IgnoreCase );
            }
            catch ( Exception )
            {
                s = oldValue;
            }

            return s;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ToString( this string source )
        {
            if ( source == null )
                return string.Empty;

            return source.ToString( CultureInfo.InvariantCulture );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns a string up to the maxLen specified.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="maxLen">The maximum length of the sting</param>
        /// <returns></returns>
        public static string ToString( this string source, int maxLen )
        {
            if ( string.IsNullOrEmpty( source ) )
                return string.Empty;

            if ( source.Length > maxLen )
                return source.Substring( 0, maxLen );

            return source.TrimEnd();
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Takes a string and if its length is greater than maxLen it trims it to maxLen-3 and 
        /// appends three periods to the string.
        /// </summary>
        /// <param name="source">the string to compare</param>
        /// <param name="maxLen">maximum length of string to return</param>
        /// <returns></returns>
        public static string ToEllipsis( this string source, int maxLen )
        {
            if ( string.IsNullOrEmpty( source ) )
                return string.Empty;

            maxLen -= 3;
            if ( source.Length > maxLen )
                return string.Format( "{0}...", source.Substring( 0, maxLen ) );

            return source.TrimEnd();
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// If possible makes a Guid from the string provided, otherwise it returns an Empty Guid.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Guid ToGuid( this string source )
        {
            Guid result;
            try
            {
                result = new Guid( source );
            }
            catch ( Exception )
            {
                result = Guid.Empty;
            }

            return result;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Converts string to title case by first changing all characters to lower case.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ToTitleCase( this string source )
        {
            CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            return cultureInfo.TextInfo.ToTitleCase( source.ToLower() );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to DateTime, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime ToDateTime( this string source, DateTime defaultValue = default( DateTime ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            DateTime result;
            if ( !DateTime.TryParse( source, out result ) )
                result = defaultValue;

            return result;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to DateTime, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static DateTime ToDate( this string source, DateTime defaultValue = default( DateTime ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            DateTime result;
            if ( !DateTime.TryParse( source, out result ) )
                result = defaultValue;

            return new DateTime( result.Year, result.Month, result.Day );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Converts string into a boolean value.
        /// </summary>
        /// <param name="source">Valid values are "f" or "false" and "t" or "true"</param>
        /// <param name="defaultValue">If parameter is not specified, false is returned.</param>
        /// <returns></returns>
        public static bool ToBool( this string source, bool defaultValue = default( bool ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            string s = source.ToLower().Trim();
            if ( s.Equals( "false" ) || s.Equals( "f" ) || s == "0" )
                return false;

            if ( s.Equals( "true" ) || s.Equals( "t" ) || s == "1" )
                return true;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to short, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static short ToShort( this string source, short defaultValue = default( short ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            short result;
            source = source.Replace( ",", "" );
            if ( short.TryParse( source, out result ) )
                return result;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to Int, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ToInt( this string source, int defaultValue = default( int ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            int result;
            source = source.Replace( ",", "" );
            if ( int.TryParse( source, out result ) )
                return result;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Trys to converts an object to an integer
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ToInt( this object source, int defaultValue = default( int ) )
        {
            int target = defaultValue;
            if ( source is string )
                target = IntParse( source.ToString() );
            else if ( source is int )
                target = (int)source;

            return target;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Converts a string to an integer 
        /// </summary>
        /// <param name="source">The string to parse</param>
        /// <param name="defaultValue">If provided, a value to return if the string cannot be parsed, 
        ///                   if not provided zero will be used as the default value</param>
        /// <returns>The parsed value if string represents a legal integer, otherwise the default value</returns>
        public static int IntParse( this string source, int defaultValue = default( int ) )
        {
            int target;
            int.TryParse( source, out target );
            return target;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to long, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static long ToLong( this string source, long defaultValue = default( long ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            long result;
            source = source.Replace( ",", "" );
            if ( long.TryParse( source, out result ) )
                return result;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to double, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static double ToDouble( this string source, double defaultValue = default( double ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            double result;
            source = source.Replace( ",", "" );
            if ( double.TryParse( source, out result ) )
                return result;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Convert string to float, if null or empty then returns defaultValue
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static float ToFloat( this string source, float defaultValue = default( float ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            float result;
            source = source.Replace( ",", "" );
            if ( float.TryParse( source, out result ) )
                return result;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Converts string to decimal, if null or empty then uses default value
        /// </summary>
        /// <param name="source"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal ToDecimal( this string source, decimal defaultValue = default( decimal ) )
        {
            if ( string.IsNullOrEmpty( source ) )
                return defaultValue;

            decimal result;
            source = source.Replace( ",", "" );
            if ( decimal.TryParse( source, out result ) )
                return result;

            return defaultValue;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        private static readonly Regex NonDigit = new Regex( "\\D" );

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns only Digits
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string OnlyDigits( this string number )
        {
            return NonDigit.Replace( number, string.Empty );
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns a short date string
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToShortDateString( this DateTime? date )
        {
            return date?.ToShortDateString() ?? string.Empty;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Returns a long date string
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToLongDateString( this DateTime? date )
        {
            return date?.ToLongDateString() ?? string.Empty;
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Provide a string to test pattern to see if email address is probably in a valid format.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsValidEmailFormat( this string source )
        {
            const string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$";
            return Regex.IsMatch( source, pattern );
        }
    }
}