Guid.ParseExact Méthode

Définition

Surcharges

Nom Description
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Convertit la représentation d’étendue de caractères d’un GUID en structure équivalente Guid , à condition que la chaîne soit au format spécifié.

ParseExact(String, String)

Convertit la représentation sous forme de chaîne d’un GUID en structure équivalente Guid , à condition que la chaîne soit au format spécifié.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Convertit la représentation d’étendue de caractères d’un GUID en structure équivalente Guid , à condition que la chaîne soit au format spécifié.

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

Paramètres

input
ReadOnlySpan<Char>

Étendue en lecture seule contenant les caractères représentant le GUID à convertir.

format
ReadOnlySpan<Char>

Étendue en lecture seule de caractères représentant l’un des spécificateurs suivants qui indique le format exact à utiliser lors de l’interprétation input: « N », « D », « B », « P » ou « X ».

Retours

Structure qui contient la valeur analysée.

Remarques

La ParseExact méthode nécessite que l’étendue de caractères en lecture seule soit convertie exactement dans le format spécifié par le format paramètre, une fois les caractères d’espace blanc de début et de fin supprimés. Le tableau suivant présente les spécificateurs de format acceptés pour le format paramètre. « 0 » représente un chiffre ; Les traits d’union (« -« ), les accolades (« { », « } ») et parenthèses (« (« , « ) ») apparaissent comme indiqué.

Spécificateur Format du input paramètre
N 32 chiffres hexadécimaux :

00000000000000000000000000000000
D 32 chiffres hexadécimaux séparés par des traits d’union :

00000000-0000-0000-0000-000000000000
B 32 chiffres hexadécimaux séparés par des traits d’union, placés entre accolades :

{00000000-0000-0000-0000-000000000000}
P 32 chiffres hexadécimaux séparés par des traits d’union, placés entre parenthèses :

(00000000-0000-0000-0000-000000000000)
X Quatre valeurs hexadécimales placées entre accolades, où la quatrième valeur est un sous-ensemble de huit valeurs hexadécimales qui sont également placées entre accolades :

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

S’applique à

ParseExact(String, String)

Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs
Source:
Guid.cs

Convertit la représentation sous forme de chaîne d’un GUID en structure équivalente Guid , à condition que la chaîne soit au format spécifié.

public:
 static Guid ParseExact(System::String ^ input, System::String ^ format);
public static Guid ParseExact(string input, string format);
static member ParseExact : string * string -> Guid
Public Shared Function ParseExact (input As String, format As String) As Guid

Paramètres

input
String

GUID à convertir.

format
String

L’un des spécificateurs suivants qui indique le format exact à utiliser lors de l’interprétation input: « N », « D », « B », « P » ou « X ».

Retours

Structure qui contient la valeur analysée.

Exceptions

input ou format est null.

input n’est pas au format spécifié par format.

Exemples

L’exemple suivant appelle la ToString(String) méthode avec chacun des spécificateurs de format pris en charge pour générer un tableau de chaînes qui représentent un GUID unique. Elles sont ensuite passées à la ParseExact méthode, qui analyse correctement la chaîne qui est conforme au spécificateur de format « B ».

// 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)
{
    try
    {
        Guid newGuid = Guid.ParseExact(stringGuid, "B");
        Console.WriteLine($"Successfully parsed {stringGuid}");
    }
    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:
//
//    Bad Format: eb5c8c7d187a44e68afb81e854c39457
//    Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457
//    Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457}
//    Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457)
//    Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}}
open System

// Define an array of all format specifiers.
let formats =
    [| "N"; "D"; "B"; "P"; "X" |]

let guid = Guid.NewGuid()

// Create an array of valid Guid string representations.
let stringGuids = 
    Array.map guid.ToString formats

// Parse the strings in the array using the "B" format specifier.
for stringGuid in stringGuids do
    try
        let newGuid = Guid.ParseExact(stringGuid, "B")
        printfn $"Successfully parsed {stringGuid}"
    with
    | :? ArgumentNullException ->
        printfn "The string to be parsed is null."
    | :? FormatException ->
        printfn $"Bad Format: {stringGuid}"

// The example displays output similar to the following:
//
//    Bad Format: eb5c8c7d187a44e68afb81e854c39457
//    Bad Format: eb5c8c7d-187a-44e6-8afb-81e854c39457
//    Successfully parsed {eb5c8c7d-187a-44e6-8afb-81e854c39457}
//    Bad Format: (eb5c8c7d-187a-44e6-8afb-81e854c39457)
//    Bad Format: {0xeb5c8c7d,0x187a,0x44e6,{0x8a,0xfb,0x81,0xe8,0x54,0xc3,0x94,0x57}}
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

      ' Parse the strings in the array using the "B" format specifier.
      For Each stringGuid In stringGuids
         Try
            Dim newGuid As Guid = Guid.ParseExact(stringGuid, "B")
            Console.WriteLine("Successfully parsed {0}", 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:
'    Bad Format: 3351d3f0006747089ff928b5179b2051
'    Bad Format: 3351d3f0-0067-4708-9ff9-28b5179b2051
'    Successfully parsed {3351d3f0-0067-4708-9ff9-28b5179b2051}
'    Bad Format: (3351d3f0-0067-4708-9ff9-28b5179b2051)
'    Bad Format: {0x3351d3f0,0x0067,0x4708,{0x9f,0xf9,0x28,0xb5,0x17,0x9b,0x20,0x51}}

Remarques

La ParseExact méthode exige que la chaîne soit convertie exactement au format spécifié par le format paramètre, une fois les caractères d’espace blanc de début et de fin supprimés. Le tableau suivant présente les spécificateurs de format acceptés pour le format paramètre. « 0 » représente un chiffre ; Les traits d’union (« -« ), les accolades (« { », « } ») et parenthèses (« (« , « ) ») apparaissent comme indiqué.

Spécificateur Format du input paramètre
N 32 chiffres hexadécimaux :

00000000000000000000000000000000
D 32 chiffres hexadécimaux séparés par des traits d’union :

00000000-0000-0000-0000-000000000000
B 32 chiffres hexadécimaux séparés par des traits d’union, placés entre accolades :

{00000000-0000-0000-0000-000000000000}
P 32 chiffres hexadécimaux séparés par des traits d’union, placés entre parenthèses :

(00000000-0000-0000-0000-000000000000)
X Quatre valeurs hexadécimales placées entre accolades, où la quatrième valeur est un sous-ensemble de huit valeurs hexadécimales qui sont également placées entre accolades :

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

Voir aussi

S’applique à