Guid.TryParseExact Guid.TryParseExact Guid.TryParseExact Guid.TryParseExact Method

定義

オーバーロード

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

文字列が指定の形式であれば、GUID の文字列形式を等価の Guid 構造体に変換します。Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, Guid) TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, 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

パラメーター

format
ReadOnlySpan<Char>
result
Guid Guid Guid Guid

戻り値

TryParseExact(String, String, Guid) TryParseExact(String, String, Guid) TryParseExact(String, String, Guid) TryParseExact(String, String, Guid)

文字列が指定の形式であれば、GUID の文字列形式を等価の Guid 構造体に変換します。Converts the string representation of a GUID to the equivalent Guid structure, provided that the string is in the specified format.

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 String String String

変換対象の GUID。The GUID to convert.

format
String String String String

input を解釈するときに使用する正確な形式を示す、"N"、"D"、"B"、"P"、"X" のいずれかの指定子。One of the following specifiers that indicates the exact format to use when interpreting input: "N", "D", "B", "P", or "X".

result
Guid Guid Guid Guid

解析された値を格納する構造体。The structure that will contain the parsed value. メソッドが true を返した場合、result には有効な Guid が格納されます。If the method returns true, result contains a valid Guid. メソッドが false を返す場合、resultEmpty と同じです。If the method returns false, result equals Empty.

戻り値

解析操作が正常に実行された場合は true。それ以外の場合は falsetrue if the parse operation was successful; otherwise, false.

次の例ではToString(String) 、サポートされている各書式指定子を使用してメソッドを呼び出し、1つの GUID を表す文字列の配列を生成します。The following example calls the ToString(String) method with each of the supported format specifiers to generate an array of strings that represent a single GUID. これらは、"B" TryParseExact書式指定子に準拠する文字列を正常に解析するメソッドに渡されます。These are then passed to the TryParseExact method, which successfully parses the string that conforms to the "B" format specifier.

using System;

public class Example
{
   public static void Main()
   {
      // 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.
      string[] 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) {
         Guid newGuid;
         if (Guid.TryParseExact(stringGuid, "B", out newGuid))
            Console.WriteLine("Successfully parsed {0}", stringGuid);
         else 
            Console.WriteLine("Unable to parse '{0}'", stringGuid);
      }     
   }
}
// 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}}'
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に、文字列がパラメーターで指定された形式で正確に変換される必要があります。This method requires the string to convert to be exactly in the format specified by the format parameter, after leading and trailing white-space characters are removed. false input formatであるか、によって指定された形式ではない場合、はを返し、は例外をスローしません。nullIt returns false if input is null or is not in the format specified by format, and doesn't throw an exception.

次の表は、 formatパラメーターで受け入れられる書式指定子を示しています。The following table shows the accepted format specifiers for the format parameter. "0" は数字を表します。ハイフン ("-")、中かっこ ("{"、"}")、かっこ ("("、")") が表示されます。"0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

指定子Specifier inputパラメーターの形式Format of the input parameter
NN 32桁:32 digits:

0000000000000000000000000000000000000000000000000000000000000000
DD 32ハイフンで区切られた数字:32 digits separated by hyphens:

00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
BB 32は、ハイフンで区切られ、中かっこで囲まれています。32 digits separated by hyphens, enclosed in braces:

{00000000-0000-0000-0000-000000000000}
PP 32数字はハイフンで区切られ、かっこで囲まれています。32 digits separated by hyphens, enclosed in parentheses:

(00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
xX 4つの16進値が中かっこで囲まれています。4番目の値は、中かっこで囲まれた8つの16進値のサブセットです。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}}{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}
こちらもご覧ください

適用対象