Skip to content

Instantly share code, notes, and snippets.

@Virtlink
Last active December 28, 2015 16:58
Show Gist options
  • Save Virtlink/7532225 to your computer and use it in GitHub Desktop.
Save Virtlink/7532225 to your computer and use it in GitHub Desktop.
Better string formatting extension methods.
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
namespace Virtlink
{
/// <summary>
/// String extension methods.
/// </summary>
/// <example>
/// Usage example:
/// <code>
/// string hello = "Hello, {0}!".FormatWith("World");
/// </code>
/// </example>
/// <remarks>
/// Created by Virtlink. Original source code on GitHub:
/// <see href="https://gist.github.com/Virtlink/7532225"/>.
/// </remarks>
public static class StringFormatting
{
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="arg0">The first argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, object arg0)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Requires<ArgumentNullException>(provider != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return String.Format(provider, format, arg0);
}
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Requires<ArgumentNullException>(provider != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return String.Format(provider, format, arg0, arg1);
}
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <param name="arg2">The third argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1, object arg2)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Requires<ArgumentNullException>(provider != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return String.Format(provider, format, arg0, arg1, arg2);
}
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="args">The arguments.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Requires<ArgumentNullException>(provider != null);
Contract.Requires<ArgumentNullException>(args != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return String.Format(provider, format, args);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, object arg0)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, arg0);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, object arg0, object arg1)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, arg0, arg1);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <param name="arg2">The third argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, object arg0, object arg1, object arg2)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, arg0, arg1, arg2);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The arguments.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, params object[] args)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Requires<ArgumentNullException>(args != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, args);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, object arg0)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, arg0);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, object arg0, object arg1)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, arg0, arg1);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <param name="arg2">The third argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, object arg0, object arg1, object arg2)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, arg0, arg1, arg2);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The arguments.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, params object[] args)
{
#region Contract
Contract.Requires<ArgumentNullException>(format != null);
Contract.Requires<ArgumentNullException>(args != null);
Contract.Ensures(Contract.Result<string>() != null);
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, args);
}
}
}
using System;
using System.Globalization;
namespace Virtlink
{
/// <summary>
/// String extension methods.
/// </summary>
/// <example>
/// Usage example:
/// <code>
/// string hello = "Hello, {0}!".FormatWith("World");
/// </code>
/// </example>
/// <remarks>
/// Created by Virtlink. Original source code on GitHub:
/// <see href="https://gist.github.com/Virtlink/7532225"/>.
/// </remarks>
public static class StringFormatting
{
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="arg0">The first argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, object arg0)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
if (provider == null) throw new NullReferenceException("provider");
#endregion
return String.Format(provider, format, arg0);
}
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
if (provider == null) throw new NullReferenceException("provider");
#endregion
return String.Format(provider, format, arg0, arg1);
}
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <param name="arg2">The third argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, object arg0, object arg1, object arg2)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
if (provider == null) throw new NullReferenceException("provider");
#endregion
return String.Format(provider, format, arg0, arg1, arg2);
}
/// <summary>
/// Formats the specified string using the specified format provider.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider to use.</param>
/// <param name="args">The arguments.</param>
/// <returns>The formatted string.</returns>
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
if (provider == null) throw new NullReferenceException("provider");
if (args == null) throw new NullReferenceException("args");
#endregion
return String.Format(provider, format, args);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, object arg0)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, arg0);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, object arg0, object arg1)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, arg0, arg1);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <param name="arg2">The third argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, object arg0, object arg1, object arg2)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, arg0, arg1, arg2);
}
/// <summary>
/// Formats the specified string using the invariant culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The arguments.</param>
/// <returns>The formatted string.</returns>
public static string FormatInvariant(this string format, params object[] args)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
if (args == null) throw new NullReferenceException("args");
#endregion
return FormatWith(format, CultureInfo.InvariantCulture, args);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, object arg0)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, arg0);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, object arg0, object arg1)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, arg0, arg1);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="arg0">The first argument.</param>
/// <param name="arg1">The second argument.</param>
/// <param name="arg2">The third argument.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, object arg0, object arg1, object arg2)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, arg0, arg1, arg2);
}
/// <summary>
/// Formats the specified string using the current culture.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="args">The arguments.</param>
/// <returns>The formatted string.</returns>
public static string FormatCurrentCulture(this string format, params object[] args)
{
#region Contract
if (format == null) throw new NullReferenceException("format");
if (args == null) throw new NullReferenceException("args");
#endregion
return FormatWith(format, CultureInfo.CurrentCulture, args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment