Guid.TryParseExact 方法

定义

重载

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

如果字符串采用指定格式,则将表示 GUID 的字符范围转换为等效的 Guid 结构。Converts span of characters representing the GUID to the equivalent Guid structure, provided that the string is in the specified format.

TryParseExact(String, String, Guid)

将 GUID 的字符串表示形式转换为等效的 Guid 结构,前提是该字符串采用的是指定格式。Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

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

如果字符串采用指定格式,则将表示 GUID 的字符范围转换为等效的 Guid 结构。Converts span of characters representing the GUID to the equivalent Guid structure, provided that the string is in the specified format.

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 的字符的只读范围。A read-only span containing the characters representing the GUID to convert.

format
ReadOnlySpan<Char>

一个包含表示以下说明符之一的字符的只读范围,这些说明符指示在解释时要使用的确切格式 input : "N"、"D"、"B"、"P" 或 "X"。A read-only span containing a character representing one of the following specifiers that indicates the exact format to use when interpreting input: "N", "D", "B", "P", or "X".

result
Guid

包含已分析的值的结构。The structure to contain the parsed value. 如果此方法返回 trueresult 将包含有效的 GuidIf the method returns true, result contains a valid Guid. 如果 result 等于 Empty,则此方法将返回 falseIf the method returns false, result equals Empty.

返回

Boolean

如果分析操作成功,则为 true;否则为 falsetrue if the parse operation was successful; otherwise, false.

适用于

产品 Introduced

TryParseExact(String, String, Guid)

将 GUID 的字符串表示形式转换为等效的 Guid 结构,前提是该字符串采用的是指定格式。Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

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);
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。The GUID to convert.

format
String

下列说明符之一,指示当解释 input 时要使用的确切格式:“N”、“D”、“B”、“P”或“X”。One of the following specifiers that indicates the exact format to use when interpreting input: "N", "D", "B", "P", or "X".

result
Guid

将包含已分析的值的结构。The structure that will contain the parsed value. 如果此方法返回 trueresult 将包含有效的 GuidIf the method returns true, result contains a valid Guid. 如果 result 等于 Empty,则此方法将返回 falseIf the method returns false, result equals Empty.

返回

Boolean

如果分析操作成功,则为 true;否则为 falsetrue if the parse operation was successful; otherwise, false.

示例

下面的示例 ToString(String) 使用每个受支持的格式说明符调用方法,以生成一个表示单个 GUID 的字符串数组。The following example calls the ToString(String) method with each of the supported format specifiers to generate an array of strings that represent a single GUID. 然后,将这些方法传递给 TryParseExact 方法,该方法成功分析符合 "B" 格式说明符的字符串。These are then passed to the TryParseExact method, which successfully parses the string that conforms to the "B" format specifier.

// 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}}'
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 删除前导和尾随空白字符后,将字符串转换为完全按照参数指定的格式。This method requires the string to convert to be exactly in the format specified by the format parameter, after leading and trailing white-space characters are removed. false如果 input 为或未 null 采用指定的格式,则它将返回 format ,且不会引发异常。It returns false if input is null or is not in the format specified by format, and doesn't throw an exception.

下表显示参数的接受格式说明符 formatThe following table shows the accepted format specifiers for the format parameter. "0" 表示一个数字;连字符 ( "-" ) 、大括号 ( "{"、"}" ) 和括号 ( " ("、") " ) 显示。"0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

说明符Specifier 参数的格式 inputFormat of the input parameter
NN 32位数:32 digits:

0000000000000000000000000000000000000000000000000000000000000000
DD 32的数字,由连字符分隔:32 digits separated by hyphens:

00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
BB 32位,用连字符隔开,括在大括号中:32 digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}
PP 32位,用连字符隔开,括在括号中:32 digits separated by hyphens, enclosed in parentheses:

(00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
XX 括在大括号中的四个十六进制值,其中第四个值是八个十六进制值的子集(也括在大括号中):Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces:

{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

另请参阅

适用于

产品 Introduced