Custom Formatting of Strings
page 2 of 2
by Pani Baruri & Abhijit Mandrekar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 16210/ 28

Custom Formatting of Strings: String.Format Implementation

The custom formatting can also be achieved through the implementation of String.Format function. In order to do so we need to implement Format method of ICustomFormatter interface. This method accepts parameters such as format string, multiple values to be formatted, and format provider object. These parameters are compatible with one of the overloaded version of String.Format function. The advantage with this function is multiple values are formatted at once in a single function call.

To start with define a class which implements interface ICustomFormatter and IFormatProvider. When call is made to Format method it first gets the instance of ICustomFormatter by calling GetFormat function of IFormatProvider interface and uses it to format multiple arguments supplied to the function.

The simple method to form a format string is to concatenate argument number followed by format string and " :" as a separator between them. The arguments are numbered starting from 0. The arguments (input strings to be formatted) are stored in an array. The order of arguments and format strings must go hand-in-hand while coding. The following code illustrates other way to custom formatting.

Class Definition:

Public Class StringFormatClass

Implements IFormatProvider

Implements ICustomFormatter

Public Function GetFormat(ByVal service As Type) As Object _

Implements IFormatProvider.GetFormat

If service.ToString() = "System.ICustomFormatter" Then

Return Me

Else

Return Nothing

End If

End Function

' After String.Format gets the ICustomFormatter, it calls this format

' method on each argument.

Public Function Format(ByVal theFormat As String, ByVal arg As Object, ByVal fp As IFormatProvider) As String _

Implements ICustomFormatter.Format

If theFormat.Equals("hhmm") Then

Return Left(arg, 2) & ":" & Right(arg, 2)

ElseIf theFormat.Equals("mmddyyyy") Then

Return Left(arg, 2) & "/" & Mid(arg, 3, 2) & "/" & Right(arg, 4)

ElseIf theFormat.Equals("9999999999") Then

Return "(" & Left(arg, 3) & ") " & Mid(arg, 4, 3) & "-" & Right(arg, 4)

Else

Return arg.ToString()

End If

End Function

End Class

User Interface Code Behind:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim formatStr As String = "{0:hhmm},{1:mmddyyyy},{2:9999999999}"

Dim stringformatObject = New StringFormatClass()

Dim args(2) As String

args(0) = TextBox1.Text

args(1) = TextBox2.Text

args(2) = TextBox3.Text

Dim myString As String = String.Format(stringformatObject, formatStr, args)

lblStringFormat.Text = myString

End Sub

How to work with this example:

Follow the same procedures as for the previous example and click button that has text "Use ICustomFormatter Interface". The result will be same however the formatted values are separated by comma. It is to the discretion of developer how to separate them and put to use appropriately.


View Entire Article

User Comments

Title: Just use Microsoft.VisualBasic.Format() Function   
Name: Anthony Missico, Jr.
Date: 2005-11-29 1:57:40 PM
Comment:
Use Microsoft.VisualBasic.Format() Function for simple string transformations.

>? Microsoft.VisualBasic.Format(now, "hhmm")
"1015"

>? Microsoft.VisualBasic.Format(now, "MMddyyyy")
"11292005"

>? Microsoft.VisualBasic.Format(Convert.ToInt64("8886664444"), "(000)000-0000")
"(888)666-4444"


The Format() function also allows the user to specify three sections in the "style" parameter. Each section allows you to format positive, negative, and null values.

>? Microsoft.VisualBasic.Format(1234, "$#,##0.00;($#,##0.00);$-.00")
"$1,234.00"

>? Microsoft.VisualBasic.Format(-4321, "$#,##0.00;($#,##0.00);$-.00")
"($4,321.00)"

>? Microsoft.VisualBasic.Format(0, "$#,##0.00;($#,##0.00);$-.00")
"$-.00"


Since you have to use statements like .ToString("hhmm") why not change it to Format(Control.Text, "hhmm") and be done with it. Save the format interfaces for complex data types. For instance, many years ago I created a StandardizedName class for use in Access Basic. I have since migrated to .NET and renamed the Format() function to ToString(). The function accepts format specifiers of LF (Last, First), LFm (Last, First M.), F (First), L (Last), FL (First Last), FmL (First M. Last), FML (First Middle Last, and so on.


See ".NET Framework Developer's Guide - Customizing Format Strings" for a better discussion, but more importantly it contains the format processing order. For instance, ICustomFormatter is called before IFormattable.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 8:51:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search