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

Format String

Shortcut for System.String.Format

Source

using System;
using System.Linq

public static class StringExtensions
{
    public static string Format(this string format, object arg, params object[] additionalArgs)
    {
        if (additionalArgs == null || additionalArgs.Length == 0)
        {
            return string.Format(format, arg);
        }
        else
        {
            return string.Format(format, new object[] { arg }.Concat(additionalArgs).ToArray());
        }
    }
}

Example

string greeting = "Hello {0}, my name is {1}, and I own you."

Console.WriteLine(greeting.Format("Adam", "Microsoft"))

Author: Adam Weigert

Submitted on: 17 dec 2007

Language: C#

Type: System.String

Views: 4324