Guid.Parse 方法

定義

多載

Parse(String, IFormatProvider)

將字串剖析成值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Char>)

將代表 GUID 的唯讀字元範圍轉換為對等的 Guid 結構。

Parse(String)

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

Parse(String, IFormatProvider)

Source:
Guid.cs
Source:
Guid.cs
Source:
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)

Source:
Guid.cs
Source:
Guid.cs
Source:
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<Char>)

Source:
Guid.cs
Source:
Guid.cs
Source:
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 並將其餘 inputGuid 字元轉換成 值。 這個方法可以轉換字元範圍,代表方法所產生的 ToString 五種格式之一,如下表所示。

規範 描述 格式
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}}

如果方法無法成功剖析字串,方法會擲 FormatException 回 。 發生此問題的一些原因包括:

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

  • input 字元太多或太少。

  • input 不是 方法所 ToString 辨識的其中一種格式,而且列在上表中。

TryParse使用 方法來攔截任何不成功的剖析作業,而不需要處理例外狀況。

適用於

Parse(String)

Source:
Guid.cs
Source:
Guid.cs
Source:
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

要轉換的字串。

傳回

包含所剖析之值的結構。

例外狀況

inputnull

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 值。 這個方法可以轉換 和 ToString(String, IFormatProvider) 方法所產生 ToString(String) 五種格式之任一格式的字串,如下表所示。

規範 描述 格式
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}}

如果方法無法成功剖析字串,方法會擲 FormatException 回 。 發生此問題的一些原因包括:

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

  • input 字元太多或太少。

  • input 不是 方法所 ToString 辨識的其中一種格式,而且列在上表中。

TryParse使用 方法來攔截任何不成功的剖析作業,而不需要處理例外狀況。

另請參閱

適用於