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)

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

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

Source:
Guid.cs
Source:
Guid.cs
Source:
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。 如果 result 等于 Empty,则此方法将返回 false

返回

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

适用于

TryParse(String, Guid)

Source:
Guid.cs
Source:
Guid.cs
Source:
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。 如果 result 等于 Empty,则此方法将返回 false

返回

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

示例

以下示例创建一个新的 GUID,通过调用 ToString(String) 带有“B”、“D”和“X”格式说明符的 方法,将其转换为三个单独的字符串表示形式,然后调用 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,不同之处在于,如果 input 是否null采用已识别格式,则返回false分析的 GUID,并且不会引发异常。 它剪裁来自 input 的任何前导空格或尾随空格,并转换由 和 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}}

另请参阅

适用于