Guid.Parse 方法

定义

重载

Parse(String, IFormatProvider)

将字符串分析为值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符范围分析为值。

Parse(ReadOnlySpan<Char>)

将表示 GUID 的只读字符范围转换为等效的 Guid 结构。

Parse(String)

将 GUID 的字符串表示形式转换为等效的 Guid 结构。

Parse(String, IFormatProvider)

将字符串分析为值。

public:
 static Guid Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<Guid>::Parse;
public static Guid Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> Guid
Public Shared Function Parse (s As String, provider As IFormatProvider) As Guid

参数

s
String

要分析的字符串。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

返回

分析 s的结果。

实现

适用于

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符范围分析为值。

public:
 static Guid Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<Guid>::Parse;
public static Guid Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> Guid
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Guid

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

返回

分析 s的结果。

实现

适用于

Parse(ReadOnlySpan<Char>)

将表示 GUID 的只读字符范围转换为等效的 Guid 结构。

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

参数

input
ReadOnlySpan<Char>

包含表示 GUID 的字节的只读范围。

返回

一个包含已分析的值的结构。

例外

input 的格式无法识别。

剪裁后,只读字符范围的长度为 0。

注解

方法 Parse 剪裁来自 input 的任何前导或尾随空格字符,并将 中的 input 剩余字符转换为 Guid 值。 此方法可以转换表示方法生成的 ToString 五种格式中的任何一种的字符范围,如下表所示。

说明符 说明 格式
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}}

如果方法无法成功分析字符串,则引发 FormatException 。 发生这种情况的一些原因包括:

  • input 包含不属于十六进制字符集的字符。

  • input 字符太多或太少。

  • input 不是方法 ToString 识别并列在上表中的格式之一。

TryParse使用 方法可捕获任何不成功的分析操作,而无需处理异常。

适用于

Parse(String)

将 GUID 的字符串表示形式转换为等效的 Guid 结构。

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

要转换的字符串。

返回

一个包含已分析的值的结构。

例外

inputnull

input 的格式无法识别。

示例

以下示例创建一个新的 GUID,通过调用 ToString(String) 带有“B”、“D”和“X”格式说明符的 方法,将其转换为三个单独的字符串表示形式,然后调用 Parse 方法将字符串转换回 Guid 值。

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
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
    try
        let newGuid = Guid.Parse stringGuid
        printfn $"Converted {stringGuid} to a Guid"
    with
    | :? ArgumentNullException ->
        printfn "The string to be parsed is null."
    | :? FormatException ->
        printfn $"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

注解

方法 Parse 剪裁来自 input 的任何前导空格或尾随空格,并将 GUID 的字符串表示形式转换为 Guid 值。 此方法可以转换 和 ToString(String, IFormatProvider) 方法生成的ToString(String)五种格式中的任何一种的字符串,如下表所示。

说明符 说明 格式
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}}

如果方法无法成功分析字符串,则引发 FormatException 。 发生这种情况的一些原因包括:

  • input 包含不属于十六进制字符集的字符。

  • input 字符太多或太少。

  • input 不是方法 ToString 识别并列在上表中的格式之一。

TryParse使用 方法可捕获任何不成功的分析操作,而无需处理异常。

另请参阅

适用于