Guid.TryParse 方法

定義

多載

名稱 Description
TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

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

TryParse(ReadOnlySpan<Byte>, Guid)
TryParse(ReadOnlySpan<Char>, Guid)

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

TryParse(String, Guid)

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

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Guid)

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

TryParse(String, IFormatProvider, Guid)

嘗試將字串剖析成值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

來源:
Guid.cs
來源:
Guid.cs
來源:
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的結果,或失敗時為未定義的值。

傳回

trues 成功解析;否則, false

適用於

TryParse(ReadOnlySpan<Byte>, Guid)

來源:
Guid.cs
來源:
Guid.cs
public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out Guid result);
static member TryParse : ReadOnlySpan<byte> * Guid -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Guid) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>
result
Guid

傳回

適用於

TryParse(ReadOnlySpan<Char>, Guid)

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

當此方法回傳時,包含解析後的值。 若方法返回 trueresult 則包含有效 Guid。 若方法回傳 falseresultEmpty等於 。

傳回

true若解析操作成功;否則,。 false

適用於

TryParse(String, Guid)

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

當此方法回傳時,包含解析後的值。 若方法返回 trueresult 則包含有效 Guid。 若方法回傳 falseresultEmpty等於 。

傳回

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

備註

此方法與 GUID Parse 類似,不同之處在於它不會回傳解析後的 GUID,而是回傳false是否inputnull以識別格式呈現,且不會拋出例外。 它會從前方或後方的空白input處裁掉,並將字串轉換為 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}}

另請參閱

適用於

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Guid)

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

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Guid

回傳時,包含成功解析 utf8Text 的結果,失敗時則為未定義的值。

傳回

trueutf8Text 成功解析;否則, false

適用於

TryParse(String, IFormatProvider, Guid)

來源:
Guid.cs
來源:
Guid.cs
來源:
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 的結果,或失敗時為未定義的值。

傳回

trues 成功解析;否則, false

適用於