Guid.TryParseExact Metodo

Definizione

Overload

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

Converte l'intervallo di caratteri che rappresenta il GUID nella struttura Guid equivalente, purché la stringa sia nel formato specificato.

TryParseExact(String, String, Guid)

Converte la rappresentazione di stringa di un GUID nella struttura Guid equivalente, purché la stringa sia nel formato specificato.

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

Origine:
Guid.cs
Origine:
Guid.cs
Origine:
Guid.cs

Converte l'intervallo di caratteri che rappresenta il GUID nella struttura Guid equivalente, purché la stringa sia nel formato specificato.

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

Parametri

input
ReadOnlySpan<Char>

Intervallo di sola lettura contenente i caratteri che rappresentano il GUID da convertire.

format
ReadOnlySpan<Char>

Intervallo di sola lettura contenente un carattere che rappresenta uno degli identificatori seguenti che indica il formato esatto da usare durante l'interpretazione di input: "N", "D", "B", "P" o "X".

result
Guid

Quando termina, questo metodo contiene il valore analizzato. Se il metodo restituisce true, result contiene un oggetto Guid valido. Se il metodo restituisce false, result è uguale a Empty.

Restituisce

true se l'operazione di analisi ha avuto esito positivo; in caso contrario, false.

Si applica a

TryParseExact(String, String, Guid)

Origine:
Guid.cs
Origine:
Guid.cs
Origine:
Guid.cs

Converte la rappresentazione di stringa di un GUID nella struttura Guid equivalente, purché la stringa sia nel formato specificato.

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

Parametri

input
String

GUID da convertire.

format
String

Uno dei seguenti identificatori, che indica il formato esatto da usare durante l'interpretazione di input: "N", "D", "B", "P" o "X".

result
Guid

Quando termina, questo metodo contiene il valore analizzato. Se il metodo restituisce true, result contiene un oggetto Guid valido. Se il metodo restituisce false, result è uguale a Empty.

Restituisce

true se l'operazione di analisi ha avuto esito positivo; in caso contrario, false.

Esempio

Nell'esempio seguente viene chiamato il ToString(String) metodo con ognuno degli identificatori di formato supportati per generare una matrice di stringhe che rappresentano un singolo GUID. Questi vengono quindi passati al TryParseExact metodo , che analizza correttamente la stringa conforme all'identificatore di formato "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}}'

Commenti

Questo metodo richiede che la stringa converta sia esattamente nel formato specificato dal format parametro , dopo la rimozione di spazi vuoti iniziali e finali. Restituisce false se input è null o non è nel formato specificato da formate non genera un'eccezione.

Nella tabella seguente vengono illustrati gli identificatori di formato accettati per il format parametro . "0" rappresenta una cifra; i trattini ("-"), le parentesi graffe ("{", "}") e le parentesi ("(", ")") vengono visualizzate come illustrato.

Identificatore Formato del input parametro
N 32 cifre:

00000000000000000000000000000000
D 32 cifre separate da trattini:

00000000-0000-0000-0000-000000000000
B 32 cifre separate da trattini, racchiuse tra parentesi graffe:

{00000000-0000-0000-0000-000000000000}
P 32 cifre separate da trattini, racchiuse tra parentesi:

(00000000-0000-0000-0000-000000000000)
X Quattro valori esadecimali racchiusi tra parentesi graffe, dove il quarto valore è un subset di otto valori esadecimali racchiusi tra parentesi graffe:

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

Vedi anche

Si applica a