Uredi

Deli z drugimi prek


Guid.Parse Method

Definition

Overloads

Parse(String, IFormatProvider)

Parses a string into a value.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parses a span of characters into a value.

Parse(String)

Converts the string representation of a GUID to the equivalent Guid structure.

Parse(ReadOnlySpan<Char>)

Converts a read-only character span that represents a GUID to the equivalent Guid structure.

Parse(String, IFormatProvider)

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

Parses a string into a value.

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

Parameters

s
String

The string to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

The result of parsing s.

Implements

Applies to

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

Parses a span of characters into a value.

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

Parameters

s
ReadOnlySpan<Char>

The span of characters to parse.

provider
IFormatProvider

An object that provides culture-specific formatting information about s.

Returns

The result of parsing s.

Implements

Applies to

Parse(String)

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

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:

  • input contains characters that are not part of the hexadecimal character set.

  • input has too many or too few characters.

  • input is not in one of the formats recognized by the ToString method and listed in the previous table.

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

See also

Applies to

Parse(ReadOnlySpan<Char>)

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

Converts a read-only character span that represents a GUID to the equivalent Guid structure.

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

Parameters

input
ReadOnlySpan<Char>

A read-only span containing the bytes representing a GUID.

Returns

A structure that contains the value that was parsed.

Exceptions

input is not in a recognized format.

-or-

After trimming, the length of the read-only character span is 0.

Remarks

The Parse method trims any leading or trailing white space characters from input and converts the remaining characters in input to a Guid value. This method can convert a character span that represents any of the five formats produced by the ToString 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:

  • input contains characters that are not part of the hexadecimal character set.

  • input has too many or too few characters.

  • input is not in one of the formats recognized by the ToString method and listed in the previous table.

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

Applies to