A cool string formatting piece of code
Published on 1/3/2008 11:00:00 PM by Philip
Today I had to come up with a way to format phone numbers from a string containing just the digits.  One solution was to just return a bunch of substring calls, but that didn't seem ".NET" enough so I poked around on the net a bit and found this idea.

http://www.thescripts.com/forum/thread361943.html

The post talks about a class that can format a string by using placeholders with substring parameters.  Sounds cool, where's the code?  I didn't find the code so I had to come up with it on my own.  (See below.)  I hope this helps you when you need to format strings.


using System;
using System.Collections.Generic;
using System.Text;

namespace Utilities
{
	/// <summary>
	/// This class returns substrings of an argument when used in a call to String.Format
	/// </summary>
	/// <remarks>
	/// Idea from (http://www.thescripts.com/forum/thread361943.html)
	/// It will use standard formatter placeholders to generate the string.
	/// The placeholders are in the form of {0:Sx,y}
	/// 0 = String.Format argument index
	/// x = Starting index of the argument
	/// y = Length desired
	/// </remarks>
	/// <example>
	/// To format 1234567890 into a phone number: String.Format(new StringFormatter(), "({0:S0,3} {0:S3,3}-{0:S6,4}", "1234567890");
	/// To format 14AB225 into an inventory location: String.Format(new StringFormatter(), "Item found in Warehouse {0:S0,2}, Room {0:S2,1}, Row {0:S3,1}, Shelf {0:S4,1}, Position {0:S5,2}", "14AB225");
	/// </example>
	public class StringFormatter : IFormatProvider, ICustomFormatter
	{
		///<summary>
		///Gets an object that provides formatting services for the specified type.
		///</summary>
		///
		///<returns>
		///The current instance, if formatType is the same type as the current instance; otherwise, null.
		///</returns>
		///
		///<param name="formatType">An object that specifies the type of format object to get. </param>
		public object GetFormat(Type formatType)
		{
			return new StringFormatter();
		}

		///<summary>
		///Converts the value of a specified object to an equivalent string representation using specified format and culture-specific formatting information.
		///</summary>
		///
		///<returns>
		///The string representation of the value of arg, formatted as specified by format and formatProvider.
		///</returns>
		///
		///<param name="arg">An object to format. </param>
		///<param name="format">A format string containing formatting specifications. </param>
		///<param name="formatProvider">An <see cref="T:System.IFormatProvider"></see> object that supplies format information about the current instance. </param>
		public string Format(string format, object arg, IFormatProvider formatProvider)
		{
			if (format.StartsWith("S"))
			{
				string[] parts = format.Remove(0, 1).Split(',');

				int start = int.Parse(parts[0]);
				int length = int.Parse(parts[1]);

				return ((String)arg).Substring(start, length);
			}

			return arg.ToString();
		}
	}
}
0 Comments
Tags:
Bookmark and Share
Design downloaded from Free Templates - your source for free web templates