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

Formating

formating the string with a custom user-defined format. # sign is input characters.

Source

/// <summary>
/// formating the string with a custom user-defined format.
/// # sign is input characters.
/// </summary>
/// <param name="format">the format string</param>
/// <returns>formated string</returns>
public static string Formating(this string input, string format)
{
    if(string.IsNullOrEmpty(input))
        return string.Empty;
    StringBuilder output = new StringBuilder();
    int j = 0;
    for(int i = 0; i < format.Length; i++)
    {
        switch(format[i])
        {
            case '#':
                output.Append(input[i - j]);
                break;
            default:
                output.Append(format[i]);
                j++;
                break;
        }
    }
    return output.ToString();
}

Example

string date="20101012";
date.Formating("####/##/##")

Author: Mojtaba kaviani

Submitted on: 14 nov. 2010

Language: C#

Type: System.String

Views: 4540