Guid.Parse Method (System) (original) (raw)

Source:

Guid.cs

Source:

Guid.cs

Source:

Guid.cs

Source:

Guid.cs

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

Parameters

input

String

The string to convert.

Returns

A structure that contains the value that was parsed.

Exceptions

input is not in a recognized format.

Examples

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.

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

Remarks

The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. 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 hexadecimal digits 00000000000000000000000000000000
D 32 hexadecimal digits separated by hyphens 00000000-0000-0000-0000-000000000000
B 32 hexadecimal digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P 32 hexadecimal digits separated by hyphens, enclosed in parentheses (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}}

The method throws a FormatException if it is unable to successfully parse the string. Some of the reasons why this might occur include:

Use the TryParse method to catch any unsuccessful parse operations without having to handle an exception.

See also

Applies to