Guid.TryParse Guid.TryParse Guid.TryParse Guid.TryParse Method

定义

重载

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

将 GUID 的字符串表示形式转换为等效的 Guid 结构。Converts the string representation of a GUID to the equivalent Guid structure.

TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, Guid) TryParse(ReadOnlySpan<Char>, 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

参数

result
Guid Guid Guid Guid

返回

TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid) TryParse(String, Guid)

将 GUID 的字符串表示形式转换为等效的 Guid 结构。Converts the string representation of a GUID to the equivalent Guid structure.

public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::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 String String String

要转换的 GUID。The GUID to convert.

result
Guid Guid Guid 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.

返回

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

示例

下面的示例创建一个新的 GUID,通过使用 "B"、"D" ToString(String)和 "X" 格式说明符调用方法,将其转换为三个单独的字符串表示形式,然后TryParse调用方法以Guid将字符串转换回分隔.The following example creates a new GUID, converts it to three separate string representations by calling the ToString(String) method with the "B", "D", and "X" format specifiers, and then calls the TryParse method to convert the strings back to Guid values.

using System;

public class Example
{
   public static void Main()
   {
      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.
      Guid newGuid;
      foreach (var stringGuid in stringGuids) {
         if (Guid.TryParse(stringGuid, out newGuid))
            Console.WriteLine("Converted {0} to a Guid", stringGuid);
         else
            Console.WriteLine("Unable to convert {0} to a Guid", 
                              stringGuid);
      }                                      
   }
}
// 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
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方法类似,不同之处在于, false它不会返回已分析的 GUID inputnull如果为或不是可识别的格式,则不会引发异常。This method is like the Parse method, except that instead of returning the parsed GUID, it returns false if input is null or not in a recognized format, and doesn't throw an exception. 它将从inputToString(String, IFormatProvider)方法所识别ToString(String)的五种格式中的任何一种格式中剪裁字符串,并转换字符串,如下表所示。It trims any leading or trailing white space from input and converts strings in any of the five formats recognized by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

说明符Specifier 说明Description 格式Format
N 32位32 digits 0000000000000000000000000000000000000000000000000000000000000000
D 32的数字,由连字符分隔32 digits separated by hyphens 00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
B 32位,用连字符隔开,括在大括号中32 digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P 由连字符分隔的32位,括在括号中32 digits separated by hyphens, enclosed in parentheses (00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
X 括在大括号中的四个十六进制值,其中第四个值是八个十六进制值的子集,这些值也括在大括号中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}}
另请参阅

适用于