ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

String Extensions

String extensions, download CoreSystem Library from codeplex; http://coresystem.codeplex.com

Source

/// <summary>
/// This class contain extension functions for string objects
/// </summary>
public static class StringExtension
{
    /// <summary>
    /// Checks string object's value to array of string values
    /// </summary>        
    /// <param name="stringValues">Array of string values to compare</param>
    /// <returns>Return true if any string value matches</returns>
    public static bool In(this string value, params string[] stringValues) {
        foreach (string otherValue in stringValues)
            if (string.Compare(value, otherValue) == 0)
                return true;

        return false;
    }

    /// <summary>
    /// Converts string to enum object
    /// </summary>
    /// <typeparam name="T">Type of enum</typeparam>
    /// <param name="value">String value to convert</param>
    /// <returns>Returns enum object</returns>
    public static T ToEnum<T>(this string value)
        where T : struct
    {
        return (T) System.Enum.Parse(typeof (T), value, true);
    }

    /// <summary>
    /// Returns characters from right of specified length
    /// </summary>
    /// <param name="value">String value</param>
    /// <param name="length">Max number of charaters to return</param>
    /// <returns>Returns string from right</returns>
    public static string Right(this string value, int length)
    {
        return value != null && value.Length > length ? value.Substring(value.Length - length) : value;
    }

    /// <summary>
    /// Returns characters from left of specified length
    /// </summary>
    /// <param name="value">String value</param>
    /// <param name="length">Max number of charaters to return</param>
    /// <returns>Returns string from left</returns>
    public static string Left(this string value, int length)
    {
        return value != null && value.Length > length ? value.Substring(0, length) : value;
    }

    /// <summary>
    ///  Replaces the format item in a specified System.String with the text equivalent
    ///  of the value of a specified System.Object instance.
    /// </summary>
    /// <param name="value">A composite format string</param>
    /// <param name="arg0">An System.Object to format</param>
    /// <returns>A copy of format in which the first format item has been replaced by the
    /// System.String equivalent of arg0</returns>
    public static string Format(this string value, object arg0)
    {
        return string.Format(value, arg0);
    }

    /// <summary>
    ///  Replaces the format item in a specified System.String with the text equivalent
    ///  of the value of a specified System.Object instance.
    /// </summary>
    /// <param name="value">A composite format string</param>
    /// <param name="args">An System.Object array containing zero or more objects to format.</param>
    /// <returns>A copy of format in which the format items have been replaced by the System.String
    /// equivalent of the corresponding instances of System.Object in args.</returns>
    public static string Format(this string value, params object[] args)
    {
        return string.Format(value, args);
    }
}

Example

//Download open source library for more extensions, database APIs, synchronization from http://coresystem.codeplex.com

var myEnum = "Pending".ToEnum<Status>();

var left = "Faraz Masood Khan".Left(100000);// works;

var right = "CoreSystem Library".Right(10);

var formatted = "My name is {0}.".Format("Faraz");

var result = "CoreSystem".In("CoreSystem", "Library");

Author: Faraz Masood Khan

Submitted on: 28 mei 2012

Language: C#

Type: System.String

Views: 26602