Guid.TryParse 方法

定義

多載

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

嘗試將字元範圍剖析為值。

TryParse(String, IFormatProvider, Guid)

嘗試將字串剖析成值。

TryParse(ReadOnlySpan<Char>, Guid)

將包含 GUID 表示法的指定唯讀字元範圍轉換為對等的 Guid 結構。

TryParse(String, Guid)

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

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

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

嘗試將字元範圍剖析為值。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] Guid % result) = ISpanParsable<Guid>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out Guid result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * Guid -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Guid) As Boolean

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

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

result
Guid

當這個方法傳回時,包含成功剖析 s 的結果,或失敗時未定義的值。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

TryParse(String, IFormatProvider, Guid)

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

嘗試將字串剖析成值。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] Guid % result) = IParsable<Guid>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out Guid result);
static member TryParse : string * IFormatProvider * Guid -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Guid) As Boolean

參數

s
String

要剖析的字串。

provider
IFormatProvider

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

result
Guid

當這個方法傳回時,包含成功剖析 s 或失敗時未定義值的結果。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

TryParse(ReadOnlySpan<Char>, Guid)

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

將包含 GUID 表示法的指定唯讀字元範圍轉換為對等的 Guid 結構。

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

參數

input
ReadOnlySpan<Char>

範圍,其包含代表所要轉換 GUID 的字元。

result
Guid

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

傳回

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

適用於

TryParse(String, Guid)

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

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

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

參數

input
String

包含所要轉換 GUID 的字串。

result
Guid

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

傳回

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

範例

下列範例會建立新的 GUID,使用 「B」、「D」 和 「X」 格式規範呼叫 ToString(String) 方法,然後將其轉換成三個不同的字串標記法,然後呼叫 TryParse 方法,將字串轉換回 Guid 值。

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)
{
    if (Guid.TryParse(stringGuid, out var newGuid))
        Console.WriteLine($"Converted {stringGuid} to a Guid");
    else
        Console.WriteLine($"Unable to convert {stringGuid} to a Guid");
}

// 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
    match Guid.TryParse stringGuid with
    | true, newGuid ->
        printfn $"Converted {stringGuid} to a Guid"
    | _ ->
        printfn $"Unable to convert {stringGuid} to a Guid"

// 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.
      Dim newGuid As Guid
      For Each stringGuid In stringGuids
         If Guid.TryParse(stringGuid, newGuid) Then
            Console.WriteLine("Converted {0} to a Guid", stringGuid)
         Else
            Console.WriteLine("Unable to convert {0} to a Guid", 
                              stringGuid)
         End If     
      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 方法一樣,不同之處在于,它不會傳回已剖析的 GUID,而是在 是 inputnull 或不是以辨識的格式傳回 ,而且不會擲回 false 例外狀況。 它會修剪 和 方法所辨識 ToString(String)ToString(String, IFormatProvider) 之五種格式中的任何一個開頭或尾端空格 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}}

另請參閱

適用於