C# String Format(String, Object)

Description

String Format(String, Object) replaces one or more format items in a specified string with the string representation of a specified object.

Syntax

String.Format(String, Object) has the following syntax.


public static string Format(
  string format,
  Object arg0
)

Parameters

String.Format(String, Object) has the following parameters.

  • format - A composite format string.
  • arg0 - The object to format.

Returns

String.Format(String, Object) method returns A copy of format in which any format items are replaced by the string representation of arg0.

Example

The following code shows how to use String.Format(String, Object) method.


/* w w w  . j a v a 2 s. c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    double temperature = 68.3;
    string result = String.Format("At {0:t} on {0:D}, the temperature was {1:F1} degrees Fahrenheit.",
                                  DateTime.Now, temperature);
    Console.WriteLine(temperature);
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version