Guid.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| 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)
- Source:
- Guid.cs
- Source:
- Guid.cs
- 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的结果或失败时未定义的值。
返回
适用于
TryParse(ReadOnlySpan<Byte>, Guid)
- Source:
- Guid.cs
- Source:
- 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)
- Source:
- Guid.cs
- Source:
- Guid.cs
- 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 的字符。
返回
true 如果分析操作成功,则为否则,为 false.
适用于
TryParse(String, Guid)
- Source:
- Guid.cs
- Source:
- Guid.cs
- 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 的字符串。
返回
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,而是返回false是否inputnull采用已识别格式,并且不会引发异常。 它剪裁任何前导空格或尾随空格input,并转换由和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)
- Source:
- Guid.cs
- Source:
- 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 的结果或失败时未定义的值。
返回
适用于
TryParse(String, IFormatProvider, Guid)
- Source:
- Guid.cs
- Source:
- Guid.cs
- 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 的结果或失败时未定义的值。