Guid.ToString 方法

定義

傳回這個 Guid 結構執行個體值的字串表示。Returns a string representation of the value of this instance of the Guid structure.

多載

ToString()

以登錄格式傳回這個執行個體的值的字串表示。Returns a string representation of the value of this instance in registry format.

ToString(String)

根據提供的格式規範,傳回這個 Guid 執行個體的值的字串表示。Returns a string representation of the value of this Guid instance, according to the provided format specifier.

ToString(String, IFormatProvider)

根據提供的格式規範和特定文化特性格式資訊,傳回這個 Guid 類別執行個體值的字串表示。Returns a string representation of the value of this instance of the Guid class, according to the provided format specifier and culture-specific format information.

ToString()

以登錄格式傳回這個執行個體的值的字串表示。Returns a string representation of the value of this instance in registry format.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

String

這個Guid的值,已使用 "D" 格式規範來格式化,如下所示:The value of this Guid, formatted by using the "D" format specifier as follows: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

其中的 GUID 值是以一系列小寫的十六進位數字表示,按照 8、4、4、4 和 12 位數分組,以連字號分開。where the value of the GUID is represented as a series of lowercase hexadecimal digits in groups of 8, 4, 4, 4, and 12 digits and separated by hyphens. 傳回值的範例是 "382c74c3-721d-4f34-80e5-57657b6cbc27"。An example of a return value is "382c74c3-721d-4f34-80e5-57657b6cbc27". 若要將從 a 到 f 的十六進位數字轉換為大寫,請呼叫所傳回之字串的 ToUpper() 方法。To convert the hexadecimal digits from a through f to uppercase, call the ToUpper() method on the returned string.

備註

這個方法提供的預設 GUID 格式已足夠供一般用途使用;不過,此方法使用參數的其他版本會 format 提供一些常見的格式變化。This method provides a default GUID format that is sufficient for typical use; however, other versions of this method that take a format parameter provide a few common format variations.

適用於

ToString(String)

根據提供的格式規範,傳回這個 Guid 執行個體的值的字串表示。Returns a string representation of the value of this Guid instance, according to the provided format specifier.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

單一格式規範,指出如何格式化這個 Guid 值。A single format specifier that indicates how to format the value of this Guid. format 參數可能是 "N"、"D"、"B"、"P" 或 "X"。The format parameter can be "N", "D", "B", "P", or "X". 如果 formatnull 或空字串 (""),則使用 "D"。If format is null or an empty string (""), "D" is used.

傳回

String

這個 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 (""), "N", "D", "B", "P", or "X".

備註

下表顯示參數接受的格式規範 formatThe 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:

0000000000000000000000000000000000000000000000000000000000000000
D 以連字號分隔的32位數:32 digits separated by hyphens:

00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
B 以連字號分隔的32位數,以大括弧括住:32 digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}
P 以連字號分隔的32位數,以括弧括住:32 digits separated by hyphens, enclosed in parentheses:

(00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
X 四個十六進位值以大括弧括住,其中第四個值是八個十六進位值的子集,也會以大括弧括住:Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces:

{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

十六進位數位 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.

適用於

ToString(String, IFormatProvider)

根據提供的格式規範和特定文化特性格式資訊,傳回這個 Guid 類別執行個體值的字串表示。Returns a string representation of the value of this instance of the Guid class, according to the provided format specifier and culture-specific format information.

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string? format, IFormatProvider? provider);
public string ToString (string format, IFormatProvider provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String

參數

format
String

單一格式規範,指出如何格式化這個 Guid 值。A single format specifier that indicates how to format the value of this Guid. format 參數可能是 "N"、"D"、"B"、"P" 或 "X"。The format parameter can be "N", "D", "B", "P", or "X". 如果 formatnull 或空字串 (""),則使用 "D"。If format is null or an empty string (""), "D" is used.

provider
IFormatProvider

(保留) 物件,提供特定文化特性格式資訊。(Reserved) An object that supplies culture-specific formatting information.

傳回

String

這個 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 (""), "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. 您可以 null 在方法呼叫中傳遞。You can pass null in the method call.

下表顯示參數接受的格式規範 formatThe 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:

0000000000000000000000000000000000000000000000000000000000000000
D 以連字號分隔的32位數:32 digits separated by hyphens:

00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
B 以連字號分隔的32位數,以大括弧括住:32 digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}
P 以連字號分隔的32位數,以括弧括住:32 digits separated by hyphens, enclosed in parentheses:

(00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
X 四個十六進位值以大括弧括住,其中第四個值是八個十六進位值的子集,也會以大括弧括住:Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces:

{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

十六進位數位 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 格式字串不支援的格式,將值表示為字串,請 String.Format(IFormatProvider, String, Object[]) 使用 provider 同時執行和介面的物件來呼叫方法 ICustomFormatter IFormatProviderTo 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.

適用於