語言

Guid.Parse 方法

定義

多載

名稱 Description
Parse(String, IFormatProvider)

將字串剖析成值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>)

將代表 GUID 的唯讀字元區間轉換為等效 Guid 的結構。

Parse(ReadOnlySpan<Byte>)
Parse(String)

將 GUID 的字串表示轉換為等價 Guid 結構。

Parse(String, IFormatProvider)

來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs

將字串剖析成值。

public:
 static Guid Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<Guid>::Parse;
public static Guid Parse(string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> Guid
Public Shared Function Parse (s As String, provider As IFormatProvider) As Guid

參數

s
String

要剖析的字串。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

解析 的結果 s。

實作

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs

將字元範圍剖析為值。

public:
 static Guid Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<Guid>::Parse;
public static Guid Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> Guid
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Guid

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

解析 的結果 s。

實作

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Guid.cs
來源:
Guid.cs

將UTF-8字元的範圍剖析為值。

public:
 static Guid Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<Guid>::Parse;
public static Guid Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> Guid
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Guid

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

provider
IFormatProvider

一個提供關於 utf8Text的文化特定格式資訊的物件。

傳回

解析 的結果 utf8Text。

實作

適用於

Parse(ReadOnlySpan<Char>)

來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs

將代表 GUID 的唯讀字元區間轉換為等效 Guid 的結構。

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

參數

input
ReadOnlySpan<Char>

一個只讀區間,包含代表 GUID 的位元組。

傳回

一個包含解析過值的結構。

例外狀況

input 並非被認可的格式。

-或-

裁剪後,唯讀字元的長度為 0。

備註

此方法會 Parse 將前方或後方的空白字元 input 從中裁掉,並將剩餘的字 input 元轉換為一個 Guid 值。 此方法可轉換為代表上述五種格式 ToString 中的任意一種字元區間,如下表所示。

指定符 Description Format
N 32 位十六進位數字 00000000000000000000000000000000
D 32 位十六進位數字,以連字號分隔 00000000-0000-0000-0000-000000000000
B 32 個十六進位數字,以連字號分隔,並以大括號包圍 {00000000-0000-0000-0000-000000000000}
P 32 位十六進位數字,以連字號分隔,括號內 (00000000-0000-0000-0000-000000000000)
X 四個十六進位值以大括號包裹,其中第四個值是八個十六進位值的子集,同樣被大括號包圍 {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

若無法成功解析字串,則拋出 a FormatException 。 可能發生這種情況的原因包括:

  • input 包含不屬於十六進位字元集的字元。

  • input 字數過多或過少。

  • input 不屬於上述表格中所識別 ToString 的格式。

使用此 TryParse 方法捕捉任何未成功的解析操作,無需處理例外。

適用於

Parse(ReadOnlySpan<Byte>)

來源:
Guid.cs
來源:
Guid.cs
public:
 static Guid Parse(ReadOnlySpan<System::Byte> utf8Text);
public static Guid Parse(ReadOnlySpan<byte> utf8Text);
static member Parse : ReadOnlySpan<byte> -> Guid
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte)) As Guid

參數

utf8Text
ReadOnlySpan<Byte>

傳回

適用於

Parse(String)

來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs
來源:
Guid.cs

將 GUID 的字串表示轉換為等價 Guid 結構。

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

要轉換的字串。

傳回

一個包含解析過值的結構。

例外狀況

input 是 null。

input 並非被認可的格式。

範例

以下範例建立一個新的 GUID,透過呼叫帶有「B」、「D」和「X」格式指定符的方法 ToString(String) ,將其轉換成三個獨立的字串表示法,然後再呼叫 Parse 將字串轉換回 Guid 數值的方法。

var 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 {stringGuid} to a Guid");
    }
    catch (ArgumentNullException)
    {
        Console.WriteLine("The string to be parsed is null.");
    }
    catch (FormatException)
    {
        Console.WriteLine($"Bad format: {stringGuid}");
    }
}

// The example displays output similar to the following:
//
//    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
open System

let originalGuid = Guid.NewGuid()
// Create an array of string representations of the GUID.
let stringGuids =
    [| originalGuid.ToString "B"
       originalGuid.ToString "D"
       originalGuid.ToString "X" |]

// Parse each string representation.
for stringGuid in stringGuids do
    try
        let newGuid = Guid.Parse stringGuid
        printfn $"Converted {stringGuid} to a Guid"
    with
    | :? ArgumentNullException ->
        printfn "The string to be parsed is null."
    | :? FormatException ->
        printfn $"Bad format: {stringGuid}"

// The example displays output similar to the following:
//
//    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

備註

此方法會 Parse 修剪任何前置或後方的空白, input 並將 GUID 的字串表示轉換為值 Guid 。 此方法可將字串轉換為 and ToString(String) 方法產生ToString(String, IFormatProvider)的五種格式中任一,如下表所示。

指定符 Description Format
N 32 位十六進位數字 00000000000000000000000000000000
D 32 位十六進位數字,以連字號分隔 00000000-0000-0000-0000-000000000000
B 32 個十六進位數字,以連字號分隔,並以大括號包圍 {00000000-0000-0000-0000-000000000000}
P 32 位十六進位數字,以連字號分隔,括號內 (00000000-0000-0000-0000-000000000000)
X 四個十六進位值以大括號包裹,其中第四個值是八個十六進位值的子集,同樣被大括號包圍 {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

若無法成功解析字串,則拋出 a FormatException 。 可能發生這種情況的原因包括:

  • input 包含不屬於十六進位字元集的字元。

  • input 字數過多或過少。

  • input 不屬於上述表格中所識別 ToString 的格式。

使用此 TryParse 方法捕捉任何未成功的解析操作,無需處理例外。

另請參閱

適用於