Guid.IFormattable.ToString(String, IFormatProvider) Method


根据所提供的格式说明符和区域性特定的格式信息,返回此实例值的字符串表示形式。Returns a string representation of the value of this instance, according to the provided format specifier and culture-specific format information.

 virtual System::String ^ System.IFormattable.ToString(System::String ^ format, IFormatProvider ^ provider) = IFormattable::ToString;
string IFormattable.ToString (string format, IFormatProvider provider);
abstract member System.IFormattable.ToString : string * IFormatProvider -> string
override this.System.IFormattable.ToString : string * IFormatProvider -> string
Function ToString (format As String, provider As IFormatProvider) As String Implements IFormattable.ToString



一个单格式说明符,它指示如何格式化此 Guid 的值。A single format specifier that indicates how to format the value of the Guid. format 参数可以是“N”、“D”、“B”、“P”或“X”。The format parameter can be "N", "D", "B", "P", or "X". 如果 format为 NULL 或空字符串 (""),则使用“D”。If format is null or an empty string (""), "D" is used.


(保留)一个对象,用于提供区域性特定的格式设置信息。(Reserved) An object that supplies culture-specific formatting information.



Guid 的值,用一系列指定格式的小写十六进制位表示。The value of this Guid represented as a series of lowercase hexadecimal digits in the specified format.



format 的值不是 NULL,一个空字符串 (""),或者下列格式说明符之一:“N”、“D”、“B”、“P”或“X”。The value of format is not null, an empty string (""), or one of the following single format specifiers:"N", "D", "B", "P", or "X".


provider 参数保留供将来使用,并且不会影响此方法的执行。The provider parameter is reserved for future use and does not contribute to the execution of this method. 可以在方法调用中传递 nullYou can pass null in the method call.

下表显示 format 参数的已接受格式说明符。The following table shows the accepted format specifiers for the format parameter. "0" 表示一个数字;连字符("-")、大括号("{"、"}")和括号("(",")")如图所示。"0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

说明符Specifier 返回值的格式Format of return value
N 32位数:32 digits:

D 32的数字,由连字符分隔:32 digits separated by hyphens:

B 32位,用连字符隔开,括在大括号中:32 digits separated by hyphens, enclosed in braces:

P 32位,用连字符隔开,括在括号中:32 digits separated by hyphens, enclosed in parentheses:

X 括在大括号中的四个十六进制值,其中第四个值是八个十六进制值的子集(也括在大括号中):Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces:


在返回的字符串中,从 a 到 f 的十六进制数字是小写的。The hexadecimal digits a through f are lowercase in the returned string. 若要将其转换为大写,请对返回的字符串调用 String.ToUpper 方法。To convert them to uppercase, call the String.ToUpper method on the returned string.

由于 provider 参数将被忽略,因此不能使用它来提供自定义格式设置解决方案。Because the provider parameter is ignored, you cannot use it to provide a custom formatting solution. 若要将 Guid 值表示为不受标准 GUID 格式字符串支持的格式的字符串,请使用实现 ICustomFormatterIFormatProvider 接口的 provider 对象调用 String.Format(IFormatProvider, String, Object[]) 方法。To represent a Guid value as a string in a format that isn't supported by the standard GUID format strings, call the String.Format(IFormatProvider, String, Object[]) method with a provider object that implements both the ICustomFormatter and IFormatProvider interfaces. 有关详细信息,请参阅格式设置一文中的"带 ICustomFormatter 的自定义格式"部分。For more information, see the "Custom Formatting with ICustomFormatter" section in the Formatting Types article.

Applies to