Guid.TryParse メソッド

定義

オーバーロード

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

文字のスパンを値に解析しようとします。

TryParse(String, IFormatProvider, Guid)

文字列を値に解析しようとします。

TryParse(ReadOnlySpan<Char>, Guid)

GUID の表現を含む文字の指定した読み取り専用のスパンを、等価の Guid 構造体に変換します。

TryParse(String, Guid)

GUID の文字列形式を、等価の Guid 構造体に変換します。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Guid)

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

文字のスパンを値に解析しようとします。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] Guid % result) = ISpanParsable<Guid>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out Guid result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * Guid -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Guid) As Boolean

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Guid

このメソッドが返されると、 には、正常に解析 sされた結果、または失敗した場合に未定義の値が格納されます。

戻り値

true が正常に解析された場合 s は 。それ以外の場合 falseは 。

適用対象

TryParse(String, IFormatProvider, Guid)

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

文字列を値に解析しようとします。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] Guid % result) = IParsable<Guid>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out Guid result);
static member TryParse : string * IFormatProvider * Guid -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Guid) As Boolean

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Guid

このメソッドが戻ったとき、 には、正常に解析 s された結果または失敗した場合に未定義の値が含まれます。

戻り値

true が正常に解析された場合 s は 。それ以外の場合 falseは 。

適用対象

TryParse(ReadOnlySpan<Char>, Guid)

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

GUID の表現を含む文字の指定した読み取り専用のスパンを、等価の Guid 構造体に変換します。

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

パラメーター

input
ReadOnlySpan<Char>

変換する GUID を表す文字を含むスパン。

result
Guid

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

戻り値

解析操作が正常に実行された場合は true。それ以外の場合は false

適用対象

TryParse(String, Guid)

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

GUID の文字列形式を、等価の Guid 構造体に変換します。

public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::Out] Guid % result);
public static bool TryParse (string input, out Guid result);
public static bool TryParse (string? input, out Guid result);
static member TryParse : string * Guid -> bool
Public Shared Function TryParse (input As String, ByRef result As Guid) As Boolean

パラメーター

input
String

変換する GUID を含む文字列。

result
Guid

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

戻り値

解析操作が正常に実行された場合は true。それ以外の場合は false

次の例では、新しい GUID を作成し、"B"、"D"、および "X" 書式指定子を使用して メソッドを呼び出 ToString(String) して 3 つの個別の文字列表現に変換し、メソッドを呼び出 TryParse して文字列を値に Guid 変換します。

Guid 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)
{
    if (Guid.TryParse(stringGuid, out var newGuid))
        Console.WriteLine($"Converted {stringGuid} to a Guid");
    else
        Console.WriteLine($"Unable to convert {stringGuid} to a Guid");
}

// 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
    match Guid.TryParse stringGuid with
    | true, newGuid ->
        printfn $"Converted {stringGuid} to a Guid"
    | _ ->
        printfn $"Unable to convert {stringGuid} to a Guid"

// 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.
      Dim newGuid As Guid
      For Each stringGuid In stringGuids
         If Guid.TryParse(stringGuid, newGuid) Then
            Console.WriteLine("Converted {0} to a Guid", stringGuid)
         Else
            Console.WriteLine("Unable to convert {0} to a Guid", 
                              stringGuid)
         End If     
      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

注釈

このメソッドは メソッドに似ていますParseが、解析された GUID を返す代わりに、 がnull認識された形式であるかどうかを返falseinputし、例外をスローしません。 次の表に示すように、 メソッドと ToString(String, IFormatProvider) メソッドによってToString(String)認識される 5 つの形式のいずれかで、先頭または末尾の空白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}}

こちらもご覧ください

適用対象