Guid.TryParseExact メソッド

定義

オーバーロード

名前 説明
TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid)

文字列が指定した形式である場合、GUID を表す文字のスパンを等価の Guid 構造体に変換します。

TryParseExact(String, String, Guid)

指定した形式の文字列である場合、GUID の文字列形式を等価の Guid 構造体に変換します。

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

文字列が指定した形式である場合、GUID を表す文字のスパンを等価の Guid 構造体に変換します。

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

パラメーター

input
ReadOnlySpan<Char>

変換する GUID を表す文字を含む読み取り専用スパン。

format
ReadOnlySpan<Char>

inputを解釈するときに使用する正確な形式を示す、次のいずれかの指定子を表す文字を含む読み取り専用スパン: "N"、"D"、"B"、"P"、または "X"。

result
Guid

このメソッドから制御が戻るときに、解析された値が格納されます。 メソッドが trueを返す場合、 result には有効な Guidが含まれます。 メソッドが falseを返す場合、 resultEmptyと等しくなります。

返品

true 解析操作が成功した場合。それ以外の場合は false

適用対象

TryParseExact(String, String, Guid)

指定した形式の文字列である場合、GUID の文字列形式を等価の Guid 構造体に変換します。

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);
static member TryParseExact : string * string * Guid -> bool
Public Shared Function TryParseExact (input As String, format As String, ByRef result As Guid) As Boolean

パラメーター

input
String

変換する GUID。

format
String

input解釈するときに使用する正確な形式を示す次のいずれかの指定子: "N"、"D"、"B"、"P"、または "X"。

result
Guid

このメソッドから制御が戻るときに、解析された値が格納されます。 メソッドが trueを返す場合、 result には有効な Guidが含まれます。 メソッドが falseを返す場合、 resultEmptyと等しくなります。

返品

true 解析操作が成功した場合。それ以外の場合は false

次の例では、サポートされている各書式指定子を使用して ToString(String) メソッドを呼び出して、1 つの GUID を表す文字列の配列を生成します。 これらは TryParseExact メソッドに渡され、"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}}'

注釈

このメソッドでは、先頭と末尾の空白文字が削除された後に、 format パラメーターで指定された形式で文字列を正確に変換する必要があります。 inputnullされている場合、またはformatで指定された形式ではない場合にfalseを返し、例外をスローしません。

次の表に、 format パラメーターに指定できる書式指定子を示します。 "0" は数字を表します。ハイフン ("-")、中かっこ ("{"、"}")、かっこ ("(",")") が次のように表示されます。

指定子 input パラメーターの形式
N 32 桁の数字:

00000000000000000000000000000000
D ハイフンで区切られた 32 桁の数字:

00000000-0000-0000-0000-000000000000
B ハイフンで区切られた 32 桁の数字(中かっこで囲む):

{00000000-0000-0000-0000-000000000000}
P かっこで囲まれたハイフンで区切られた 32 桁の数字:

(00000000-0000-0000-0000-000000000000)
X 中かっこで囲まれた 4 つの 16 進値。4 番目の値は、中かっこで囲まれた 8 つの 16 進値のサブセットです。

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

こちらもご覧ください

適用対象