Guid.TryParseExact Méthode

Définition

Surcharges

Nom Description
TryParseExact(String, String, Guid)

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é.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)

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

TryParseExact(String, String, Guid)

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 bool TryParseExact(System::String ^ input, System::String ^ format, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParseExact(string input, string format, out Guid result);
public static bool TryParseExact(string? input, string? format, out Guid result);
static member TryParseExact : string * string * Guid -> bool
Public Shared Function TryParseExact (input As String, format As String, ByRef result As Guid) As Boolean

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 ».

result
Guid

Lorsque cette méthode est retournée, contient la valeur analysée. Si la méthode retourne true, result contient une valeur valide Guid. Si la méthode retourne false, result est Emptyégale .

Retours

true si l’opération d’analyse a réussi ; sinon, false.

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. Ils sont ensuite passés à la TryParseExact méthode, qui analyse avec succès la chaîne 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)
{
    if (Guid.TryParseExact(stringGuid, "B", out var newGuid))
        Console.WriteLine($"Successfully parsed {stringGuid}");
    else
        Console.WriteLine($"Unable to parse '{stringGuid}'");
}

// The example displays output similar to the following:
//
//    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
//    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
//    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
//    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
//    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'
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
    match Guid.TryParseExact(stringGuid, "B") with
    | true, newGuid ->
        printfn $"Successfully parsed {stringGuid}"
    | _ ->
        printfn $"Unable to parse '{stringGuid}'"

// The example displays output similar to the following:
//
//    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
//    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
//    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
//    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
//    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'
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

      ' Try to parse the strings in the array using the "B" format specifier.
      For Each stringGuid In stringGuids
         Dim newGuid As Guid
         If Guid.TryParseExact(stringGuid, "B", newGuid) Then
            Console.WriteLine("Successfully parsed {0}", stringGuid)
         Else
            Console.WriteLine("Unable to parse '{0}'", stringGuid)
         End If   
      Next      
   End Sub
End Module
' The example displays the following output:
'    Unable to parse 'c0fb150f6bf344df984a3a0611ae5e4a'
'    Unable to parse 'c0fb150f-6bf3-44df-984a-3a0611ae5e4a'
'    Successfully parsed {c0fb150f-6bf3-44df-984a-3a0611ae5e4a}
'    Unable to parse '(c0fb150f-6bf3-44df-984a-3a0611ae5e4a)'
'    Unable to parse '{0xc0fb150f,0x6bf3,0x44df,{0x98,0x4a,0x3a,0x06,0x11,0xae,0x5e,0x4a}}'

Remarques

Cette méthode nécessite que la chaîne soit convertie exactement dans le format spécifié par le format paramètre, une fois les espaces blancs de début et de fin supprimés. Elle retourne false si input elle est ou n’est null pas dans le format spécifié par format, et ne lève pas d’exception.

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 :

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

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

{00000000-0000-0000-0000-000000000000}
P 32 chiffres 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 à

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)

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

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

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, out Guid result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * Guid -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), ByRef result As Guid) As Boolean

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 contenant un caractère 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 ».

result
Guid

Lorsque cette méthode est retournée, contient la valeur analysée. Si la méthode retourne true, result contient une valeur valide Guid. Si la méthode retourne false, result est Emptyégale .

Retours

true si l’opération d’analyse a réussi ; sinon, false.

S’applique à