|
|
|
|
|
Wednesday, 14 December 2011 02:05 |
The string.Format method is a static method that receives a string that specifies where the following arguments should be inserted, and these are called substitutions
Following example shows the use of the string.Format method to combine three strings with formatting options. The format string itself is the first argument to the string.Format method and it is specified as a string literal.
The position markers in the format string are specified to the left of the colons, and this means the "0", "1:", and "2:" indicate where the first, second, and third arguments are inserted. The part after the ":" and in between the { } brackets indicates the exact format specification for that variable.
String Literal
static void Main()
{
//
// Declare three variables that we will use in the format method.
// ... The values they have are not important.
//
string value1 = "This is test";
int value2 = 530;
DateTime value3 = new DateTime(2011, 11, 11);
//
// Use string.Format method with four arguments.
// ... The first argument is the formatting string.
// ... It specifies how the next three arguments are formatted.
//
string result = string.Format("{0}: {1:0.0} - {2:yyyy}",value1,value2,value3);
Console.WriteLine(result);
}
/*
OUTPUT
This is test: 530.0 - 2011
*/
Number formats
You can specify that a value type such as a double can be formatted inside the string.Format method based on the format string. The format string is the first argument to the string.Format method. The format string in this example uses the 0:0.0% syntax, which means that the second argument should be formatted with the pattern 0.0%. The arguments are numbered starting at zero.
static void Main()
{
//
// Format a ratio as a percentage string.
// ... You must specify the percentage symbol in the format string.
// ... It will multiply the value by 100 for you.
//
double ratio = 0.530;
string result = string.Format("string = {0:0.0%}",ratio);
Console.WriteLine(result);
}
/*
OUTPUT
string = 530.0%
*/
Padding (PadLeft and PadRight)
Padding can be used with strings in the C# language and this can be expressed declaratively in formatting strings and the string.Format method. The term 'padding' indicates that you are inserting a variable number of characters at the left or right of the string to ensure that the total length of the string is a fixed length. Instead of the PadLeft and PadRight methods, you can use the string.Format method with special substitutions.
static void Main()
{
//
// The constant formatting string.
// ... It specifies the padding.
// ... A negative number means to left-align.
// ... A positive number means to right-align.
//
const string format = "{0,-10} {1,10}";
//
// Construct the strings.
//
string line1 = string.Format(format,100,5);
string line2 = string.Format(format,"Carrot","Giraffe");
Console.WriteLine(line1);
Console.WriteLine(line2);
}
/*
OUTPUT
100 5
Carrot Giraffe
*/
ToString or string.Format
Sometimes, you need to just format a single number, like an integer or long. In this case, you don't need to use string.Format. You can just use the ToString virtual method.
static void Main()
{
int value = 123;
string a = string.Format("{0:0000}", value); // Too complex
string b = value.ToString("0000"); // Simpler
Console.WriteLine(a);
Console.WriteLine(b);
}
/*
OUTPUT
0123
0123
*/
DateTime Format
format string pattern defined as followse
MMM display three-letter month
ddd display three-letter day of the WEEK
d display day of the MONTH
HH display two-digit hours on 24-hour scale
mm display two-digit minutes
yyyy display four-digit year
static void Main()
{
DateTime time = DateTime.Now; // Use current time
string format = "MMM ddd d HH:mm yyyy"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console
}
/*
OUTPUT
Feb Fri 27 11:41 2009
*/
 Read more: |
|
|
Monday, 24 January 2011 12:32 |
a little helper function which parses about any date and returns the same date in MySQL date/time format Download Wow Hits 2010 2011
function date_to_mysql($date) {
//if $date is not numeric, assume it's in textual format
if (!is_numeric($date)) {
$date=strtotime($date);
}
return date('Y-m-d H:i:s',$date);
}
 Read more: |
|
|
|
|
|
|
Page 1 of 2 |