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

ToSentence

Creates a sentence from a variable name.

Source

public static string ToSentence(this string variableName)
{
    var builder = new StringBuilder();

    char[] chars = variableName.ToCharArray();

    foreach (char c in chars)
    {
        if (char.IsLetter(c) && char.IsUpper(c))
        {
            builder.Append(" ");
        }

        builder.Append(c);
    }

    variableName = builder.ToString().TrimStart();

    return variableName;
}

Example

Create an extension method on a DataGrid to pretty print the column names.

public static void SetLabelsToSentences(
    this XamDataGrid xamDataGrid)
{
    foreach (
        Field field in xamDataGrid.FieldLayouts[0].Fields)
    {
        xamDataGrid.SetLabel(
            field.Name, field.Name.ToSentence());
    }
}

Author: Richard Bogle

Submitted on: 30 sep. 2009

Language: C#

Type: System.String

Views: 5539