Share via


IFormattable.ToString 方法

使用指定的格式格式化当前实例的值。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Function ToString ( _
    format As String, _
    formatProvider As IFormatProvider _
) As String
用法
Dim instance As IFormattable
Dim format As String
Dim formatProvider As IFormatProvider
Dim returnValue As String

returnValue = instance.ToString(format, formatProvider)
string ToString (
    string format,
    IFormatProvider formatProvider
)
String^ ToString (
    String^ format, 
    IFormatProvider^ formatProvider
)
String ToString (
    String format, 
    IFormatProvider formatProvider
)
function ToString (
    format : String, 
    formatProvider : IFormatProvider
) : String

参数

  • format
    指定要使用的格式的 String

    - 或 -

    空引用(在 Visual Basic 中为 Nothing),表示使用为 IFormattable 实现的类型定义的默认格式。

  • formatProvider
    用于格式化该值的 IFormatProvider

    - 或 -

    从操作系统的当前区域设置中获取数字格式信息的 空引用(在 Visual Basic 中为 Nothing)。

返回值

一个 String,包含具有指定格式的当前实例的值。

备注

NumberFormatInfoDateTimeFormatInfoCultureInfo 实现 IFormatProvider 接口。

NumberFormatInfo 提供数字格式信息,如用于小数分隔符和千位分隔符的字符,以及货币值中货币符号的拼写和位置。

DateTimeFormatInfo 提供与日期相关和与时间相关的格式信息,如日期模式中月、日和年的位置。

CultureInfo 包含特定区域性中的默认格式信息,其中包括数字格式信息以及与日期相关和与时间相关的格式信息。

示例

下面的代码示例演示如何实现 ToString 方法。此代码示例摘自一个为 IFormattable 类提供的更大的示例。

public override String ToString() { return ToString(null, null); }

public String ToString(String format, IFormatProvider fp)
{
    // If no format is passed, display like this: (x, y).
    if (format == null) return String.Format("({0}, {1})", x, y);

    // For "x" formatting, return just the x value as a string
    if (format == "x") return x.ToString();

    // For "y" formatting, return just the y value as a string
    if (format == "y") return y.ToString();

    // For any unrecognized format, throw an exception.
    throw new FormatException(String.Format("Invalid format string: '{0}'.", format));
}
public:
    virtual String^ ToString() override
    {
        return ToString(nullptr, nullptr);
    }

public:
    virtual String^ ToString(String^ format, IFormatProvider^ formatProvider)
    {
        // If no format is passed, display like this: (x, y).
        if (format == nullptr)
        {
            return String::Format("({0}, {1})", x, y);
        }

        // For "x" formatting, return just the x value as a string
        if (format == "x")
        {
            return x.ToString();
        }

        // For "y" formatting, return just the y value as a string
        if (format == "y")
        {
            return y.ToString();
        }

        // For any unrecognized format, throw an exception.
        throw gcnew FormatException(String::Format(
            "Invalid format string: '{0}'.", format));
    }

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

IFormattable 接口
IFormattable 成员
System 命名空间
String
IFormatProvider 接口
System.Globalization.NumberFormatInfo
System.Globalization.DateTimeFormatInfo
System.Globalization.CultureInfo