Guid.Parse Guid.Parse Guid.Parse Guid.Parse Method

定義

多載

Parse(String) Parse(String) Parse(String) Parse(String)

將 GUID 的字串表示轉換為對等的 Guid 結構。Converts the string representation of a GUID to the equivalent Guid structure.

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

Parse(String) Parse(String) Parse(String) Parse(String)

將 GUID 的字串表示轉換為對等的 Guid 結構。Converts the string representation of a GUID to the equivalent Guid structure.

public:
 static Guid Parse(System::String ^ input);
public static Guid Parse (string input);
static member Parse : string -> Guid
Public Shared Function Parse (input As String) As Guid

參數

input
String String String String

要轉換的字串。The string to convert.

傳回

包含所剖析之值的結構。A structure that contains the value that was parsed.

例外狀況

input 的格式無法辨識。input is not in a recognized format.

範例

下列範例會建立新的 GUID,並藉由呼叫ToString(String)具有 "B"、"D" 和 "X" 格式規範的方法,將它轉換成三個不同的字串表示,然後Parse呼叫方法,將字串Guid轉換回閾值.The following example creates a new GUID, converts it to three separate string representations by calling the ToString(String) method with the "B", "D", and "X" format specifiers, and then calls the Parse method to convert the strings back to Guid values.

using System;

public class Example
{
   public static void Main()
   {
      Guid originalGuid = Guid.NewGuid();
      // Create an array of string representations of the GUID.
      string[] stringGuids = { originalGuid.ToString("B"),
                               originalGuid.ToString("D"),
                               originalGuid.ToString("X") };
      
      // Parse each string representation.
      foreach (var stringGuid in stringGuids) {
         try {     
            Guid newGuid = Guid.Parse(stringGuid);
            Console.WriteLine("Converted {0} to a Guid", stringGuid);
         }   
         catch (ArgumentNullException) {
            Console.WriteLine("The string to be parsed is null.");   
         }                                   
         catch (FormatException) {
            Console.WriteLine("Bad format: {0}", stringGuid);
         }
      }                                      
   }
}
// The example displays the following output:
//    Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid
//    Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid
//    Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid
Module Example
   Public Sub Main()
      Dim originalGuid As Guid = Guid.NewGuid()
      ' Create an array of string representations of the GUID.
      Dim stringGuids() As String = { originalGuid.ToString("B"),
                                      originalGuid.ToString("D"),
                                      originalGuid.ToString("X") }
      
      ' Parse each string representation.
      For Each stringGuid In stringGuids
         Try 
            Dim newGuid As Guid = Guid.Parse(stringGuid) 
            Console.WriteLine("Converted {0} to a Guid", stringGuid)
         Catch e As ArgumentNullException
            Console.WriteLine("The string to be parsed is null.")   
         Catch e As FormatException
            Console.WriteLine("Bad format: {0}", stringGuid)
         End Try     
      Next                                      
   End Sub
End Module
' The example displays the following output:
'    Converted {81a130d2-502f-4cf1-a376-63edeb000e9f} to a Guid
'    Converted 81a130d2-502f-4cf1-a376-63edeb000e9f to a Guid
'    Converted {0x81a130d2,0x502f,0x4cf1,{0xa3,0x76,0x63,0xed,0xeb,0x00,0x0e,0x9f}} to a Guid

備註

方法會修剪中input的任何前置或尾端空白字元,並將 GUID 的字串表示轉換為Guid值。 ParseThe Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. 這個方法可以使用ToString(String)ToString(String, IFormatProvider)方法所產生的五種格式之一來轉換字串,如下表所示。This method can convert strings in any of the five formats produced by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

指定名稱Specifier 說明Description 格式Format
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}}

FormatException如果方法無法成功剖析字串,則會擲回。The method throws a FormatException if it is unable to successfully parse the string. 以下是可能發生這種情況的一些原因:Here are some of the reasons why this might occur include:

  • input包含不屬於十六進位字元集的字元。input contains characters that are not part of the hexadecimal character set.

  • input有太多或太少的數值字元。input has too many or too few numeric characters.

  • input有太多或太少不適合特定格式的非數位字元。input has too many or too few of the non-numeric characters appropriate for a particular format.

  • input不是ToString方法所辨識並列在上表中的其中一種格式。input is not in one of the formats recognized by the ToString method and listed in the previous table.

TryParse使用方法來攔截任何不成功的剖析作業,而不需要處理例外狀況。Use the TryParse method to catch any unsuccessful parse operations without having to handle an exception.

另請參閱

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

public:
 static Guid Parse(ReadOnlySpan<char> input);
public static Guid Parse (ReadOnlySpan<char> input);
static member Parse : ReadOnlySpan<char> -> Guid
Public Shared Function Parse (input As ReadOnlySpan(Of Char)) As Guid

參數

傳回

適用於