Guid.TryParseExact 方法

定義

多載

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)

將代表 GUID 的字元範圍轉換為對等的 Guid 結構,但字串必須是指定的格式。

TryParseExact(String, String, Guid)

將 GUID 的字串表示轉換為對等的 Guid 結構,但字串必須是指定的格式。

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

將代表 GUID 的字元範圍轉換為對等的 Guid 結構,但字串必須是指定的格式。

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, out Guid result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * Guid -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), ByRef result As Guid) As Boolean

參數

input
ReadOnlySpan<Char>

包含代表要轉換之 GUID 之位元組的唯讀範圍。

format
ReadOnlySpan<Char>

唯讀範圍,包含代表下列其中一個規範的字元,表示解譯 input 時要使用的確切格式:「N」、「D」、「B」、「P」 或 「X」。

result
Guid

當這個方法傳回時,會包含剖析的值。 如果方法傳回 true,則 result 包含有效的 Guid。 如果方法會傳回 false,則 result 等於 Empty

傳回

如果剖析作業成功則為 true,否則為 false

適用於

TryParseExact(String, String, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

將 GUID 的字串表示轉換為對等的 Guid 結構,但字串必須是指定的格式。

public:
 static bool TryParseExact(System::String ^ input, System::String ^ format, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParseExact (string input, string format, out Guid result);
public static bool TryParseExact (string? input, string? format, out Guid result);
static member TryParseExact : string * string * Guid -> bool
Public Shared Function TryParseExact (input As String, format As String, ByRef result As Guid) As Boolean

參數

input
String

要轉換的 GUID。

format
String

下列其中一個規範,表示在解譯 input 時要使用的確切格式:"N"、"D"、"B"、"P" 或 "X"。

result
Guid

當這個方法傳回時,會包含剖析的值。 如果方法傳回 true,則 result 包含有效的 Guid。 如果方法會傳回 false,則 result 等於 Empty

傳回

如果剖析作業成功則為 true,否則為 false

範例

下列範例會 ToString(String) 使用每個支援的格式規範呼叫 方法,以產生代表單一 GUID 的字串陣列。 這些接著會傳遞至 TryParseExact 方法,以成功剖析符合 「B」 格式規範的字串。

// Define an array of all format specifiers.
string[] formats = { "N", "D", "B", "P", "X" };
Guid guid = Guid.NewGuid();
// Create an array of valid Guid string representations.
var stringGuids = new string[formats.Length];
for (int ctr = 0; ctr < formats.Length; ctr++)
    stringGuids[ctr] = guid.ToString(formats[ctr]);

// Parse the strings in the array using the "B" format specifier.
foreach (var stringGuid in stringGuids)
{
    if (Guid.TryParseExact(stringGuid, "B", out var newGuid))
        Console.WriteLine($"Successfully parsed {stringGuid}");
    else
        Console.WriteLine($"Unable to parse '{stringGuid}'");
}

// The example displays output similar to the following:
//
//    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
//    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
//    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
//    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
//    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'
open System

// Define an array of all format specifiers.
let formats = [| "N"; "D"; "B"; "P"; "X" |]

let guid = Guid.NewGuid()

// Create an array of valid Guid string representations.
let stringGuids = 
    Array.map guid.ToString formats

// Parse the strings in the array using the "B" format specifier.
for stringGuid in stringGuids do
    match Guid.TryParseExact(stringGuid, "B") with
    | true, newGuid ->
        printfn $"Successfully parsed {stringGuid}"
    | _ ->
        printfn $"Unable to parse '{stringGuid}'"

// The example displays output similar to the following:
//
//    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
//    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
//    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
//    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
//    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'
Module Example
   Public Sub Main()
      ' Define an array of all format specifiers.
      Dim formats() As String = { "N", "D", "B", "P", "X" }
      Dim guid As Guid = Guid.NewGuid()
      ' Create an array of valid Guid string representations.
      Dim stringGuids(formats.Length - 1) As String
      For ctr As Integer = 0 To formats.Length - 1
         stringGuids(ctr) = guid.ToString(formats(ctr))
      Next

      ' Try to parse the strings in the array using the "B" format specifier.
      For Each stringGuid In stringGuids
         Dim newGuid As Guid
         If Guid.TryParseExact(stringGuid, "B", newGuid) Then
            Console.WriteLine("Successfully parsed {0}", stringGuid)
         Else
            Console.WriteLine("Unable to parse '{0}'", stringGuid)
         End If   
      Next      
   End Sub
End Module
' The example displays the following output:
'    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
'    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
'    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
'    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
'    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'

備註

這個方法要求字串在移除開頭和尾端空白字元之後,以參數指定的 format 格式完全轉換。 如果 inputnull 或 不是 所 format 指定的格式,則傳回 ,而且不會擲回 false 例外狀況。

下表顯示 參數的已接受格式規範 format 。 「0」 代表數位;連字號 (「-」) 、大括弧 (「{」、「}」) 和括弧 (「 (」、「) 」) 如下所示。

規範 input參數的格式
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}}

另請參閱

適用於