The String.Format Method...
The .NET framework has added a lot of useful string manipulation functions, which one of them is the Format method.
The .NET framework has added a lot of useful string manipulation functions, which one of them is the Format method. This method performs manipulation easily through a placeholder, which is a numerical value, surrounded by curly braces, in the format of {n}. The number must be a non-negative zero-based number, which identifies the replacement value to use on its behalf. For example, the code:
String.Format("The current date is {0}.", DateTime.Now.ToShortDateString()
Returns "The current date is 5/16/2004". This function takes the placeholder {0} and replaces it with the object passed into the function. The first three overloaded methods accept one to three values as replacements. The other overloaded methods are shown in the example below:
String.Format("Page {0} of {1}", intCurrentPage, intMaxPages)
String.Format("DSN={0};User ID={1};Password={2};", strDSN, strUserID, strPwd)
The placeholders operate in numerical order, so the zero placeholder receives the first variable, the one placeholder receives the second variable, and so on. It is important to note that if a placeholder is left out, the variable designated for that placeholder is ignored. Also, if more than three placeholders are desired, another overloaded method supports an array of objects that can be used, as shown below. This method works in the same manner; each value in the array is associated with a placeholder starting from zero, and incrementing by one for each array entry.
String.Format("Top ten list: {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}", strTopTenArray)
This document is short and brief, but is meant to illustrate the usefulness of the Format method.