Guid.Parse Guid.Parse Guid.Parse Guid.Parse Method

定义

重载

Parse(String) Parse(String) Parse(String) Parse(String)

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

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

Parse(String) Parse(String) Parse(String) 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

参数

input
String String String String

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

返回

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

异常

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

示例

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

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

注解

方法将从中input剪裁任意前导空格或尾随空格,并将 GUID 的字符串表示形式转换Guid为值。 ParseThe 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}}

FormatException如果无法成功分析字符串,方法将引发。The 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.

另请参阅

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

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

参数

返回

适用于