Guid.TryParseExact 方法

定义

重载

名称 说明
TryParseExact(String, String, Guid)

将 GUID 的字符串表示形式转换为等效 Guid 结构,前提是字符串采用指定格式。

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

将表示 GUID 的字符范围转换为等效 Guid 结构,前提是字符串采用指定格式。

TryParseExact(String, String, Guid)

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

此方法返回时,包含已分析的值。 如果方法返回 trueresult 则包含有效的 Guid。 如果方法返回 falseresult 则等于 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 格式进行转换。 它返回false是否inputnull采用指定的format格式,并且不会引发异常。

下表显示了参数的接受格式说明符 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}}

另请参阅

适用于

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

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

此方法返回时,包含已分析的值。 如果方法返回 trueresult 则包含有效的 Guid。 如果方法返回 falseresult 则等于 Empty

返回

true 如果分析操作成功,则为否则,为 false.

适用于