Guid.Parse Method

Definition

Overloads

Parse(String)

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

Parse(ReadOnlySpan<Char>)

将表示 GUID 的只读字符范围转换为等效的 Guid 结构。Converts a read-only character span that represents a GUID to the equivalent Guid structure.

Parse(String)

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

public:
 static Guid Parse(System::String ^ input);
public static Guid Parse (string input);
static member Parse : string -> Guid
Public Shared Function Parse (input As String) As Guid

Parameters

input
String

要转换的字符串。The string to convert.

Returns

Guid

一个包含已分析的值的结构。A structure that contains the value that was parsed.

Exceptions

inputnullinput is null.

input 的格式无法识别。input is not in a recognized format.

Examples

下面的示例创建一个新的 GUID,通过使用 "B"、"D" 和 "X" 格式说明符调用 ToString(String) 方法,将其转换为三个单独的字符串表示形式,然后调用 Parse 方法将字符串转换回 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 Parse method to convert the strings back to Guid values.

var 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)
{
    try
    {
        Guid newGuid = Guid.Parse(stringGuid);
        Console.WriteLine($"Converted {stringGuid} to a Guid");
    }
    catch (ArgumentNullException)
    {
        Console.WriteLine("The string to be parsed is null.");
    }
    catch (FormatException)
    {
        Console.WriteLine($"Bad format: {stringGuid}");
    }
}

// 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.
      For Each stringGuid In stringGuids
         Try 
            Dim newGuid As Guid = Guid.Parse(stringGuid) 
            Console.WriteLine("Converted {0} to a Guid", stringGuid)
         Catch e As ArgumentNullException
            Console.WriteLine("The string to be parsed is null.")   
         Catch e As FormatException
            Console.WriteLine("Bad format: {0}", stringGuid)
         End Try     
      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

Remarks

Parse 方法将从 input 中剪裁任何前导空格或尾随空格,并将 GUID 的字符串表示形式转换为 Guid 值。The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. 此方法可以转换由 ToString(String)ToString(String, IFormatProvider) 方法生成的五种格式中的任何一种格式的字符串,如下表所示。This method can convert strings in any of the five formats produced 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}}

如果方法无法成功分析字符串,则会引发 FormatExceptionThe method throws a FormatException if it is unable to successfully parse the string. 下面是可能出现此情况的一些原因:Here are some of the reasons why this might occur include:

  • input 包含不属于十六进制字符集的字符。input contains characters that are not part of the hexadecimal character set.

  • input 包含太多或太少的数字字符。input has too many or too few numeric characters.

  • input 包含的非数字字符太多或太少,不适用于特定格式。input has too many or too few of the non-numeric characters appropriate for a particular format.

  • input 不是 ToString 方法识别的格式之一,并在上表中列出。input is not in one of the formats recognized by the ToString method and listed in the previous table.

使用 TryParse 方法可捕获所有不成功的分析操作,而无需处理异常。Use the TryParse method to catch any unsuccessful parse operations without having to handle an exception.

See also

Parse(ReadOnlySpan<Char>)

将表示 GUID 的只读字符范围转换为等效的 Guid 结构。Converts a read-only character span that represents a GUID to the equivalent Guid structure.

public:
 static Guid Parse(ReadOnlySpan<char> input);
public static Guid Parse (ReadOnlySpan<char> input);
static member Parse : ReadOnlySpan<char> -> Guid
Public Shared Function Parse (input As ReadOnlySpan(Of Char)) As Guid

Parameters

input
ReadOnlySpan<Char>

包含表示 GUID 的字节的只读范围。A read-only span containing the bytes representing a GUID.

Returns

Guid

一个包含已分析的值的结构。A structure that contains the value that was parsed.

Exceptions

input 的格式无法识别。input is not in a recognized format.

- 或 --or-

剪裁后,只读字符范围的长度为 0。After trimming, the length of the read-only character span is 0.

Remarks

Parse 方法将从 input 中剪裁任何前导空格或尾随空格字符,并将 input 中剩余的字符转换为 Guid 值。The Parse method trims any leading or trailing white space characters from input and converts the remaining characters in input to a Guid value. 此方法可以转换表示由 ToString 方法生成的五种格式中的任何一种格式的字符范围,如下表所示。This method can convert a character span that represents any of the five formats produced by the ToString 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}}

如果方法无法成功分析字符串,则会引发 FormatExceptionThe method throws a FormatException if it is unable to successfully parse the string. 下面是可能出现此情况的一些原因:Here are some of the reasons why this might occur include:

  • input 包含不属于十六进制字符集的字符。input contains characters that are not part of the hexadecimal character set.

  • input 包含太多或太少的数字字符。input has too many or too few numeric characters.

  • input 包含的非数字字符太多或太少,不适用于特定格式。input has too many or too few of the non-numeric characters appropriate for a particular format.

  • input 不是 ToString 方法识别的格式之一,并在上表中列出。input is not in one of the formats recognized by the ToString method and listed in the previous table.

使用 TryParse 方法可捕获所有不成功的分析操作,而无需处理异常。Use the TryParse method to catch any unsuccessful parse operations without having to handle an exception.

Applies to