Guid.Parse Guid.Parse Guid.Parse Guid.Parse Method

定義

オーバーロード

Parse(String) Parse(String) Parse(String) Parse(String)

GUID の文字列形式を、等価の Guid 構造体に変換します。Converts the string representation of a GUID to the equivalent Guid structure.

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

Parse(String) Parse(String) Parse(String) Parse(String)

GUID の文字列形式を、等価の Guid 構造体に変換します。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

パラメーター

input
String String String String

変換する文字列。The string to convert.

戻り値

解析された値を格納する構造体。A structure that contains the value that was parsed.

例外

input が、認識される形式ではありません。input is not in a recognized format.

次の例では、新しい GUID を作成、呼び出すことによって 3 つの別個の文字列表現に変換、ToString(String)メソッドで、"B"、"D"と"X"書式指定子と、呼び出し、Parseに、文字列に変換するメソッドが戻るGuid値。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.

using System;

public class Example
{
   public static void Main()
   {
      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) {
         try {     
            Guid newGuid = Guid.Parse(stringGuid);
            Console.WriteLine("Converted {0} to a Guid", stringGuid);
         }   
         catch (ArgumentNullException) {
            Console.WriteLine("The string to be parsed is null.");   
         }                                   
         catch (FormatException) {
            Console.WriteLine("Bad format: {0}", stringGuid);
         }
      }                                      
   }
}
// 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
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

注釈

Parseメソッドからの先頭または末尾にある空白文字のトリムinputには、GUID の文字列形式に変換し、Guid値。The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. このメソッドによって生成された 5 つの形式のいずれかで文字列を変換できる、ToString(String)ToString(String, IFormatProvider)メソッドは、次の表に示すようにします。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 桁の数字32 digits 0000000000000000000000000000000000000000000000000000000000000000
D ハイフンで区切られた 32 桁の数字32 digits separated by hyphens 00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000
B 中かっこで囲まれ、ハイフンで区切られた 32 桁の数字32 digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P かっこで囲まれ、ハイフンで区切られた 32 桁の数字32 digits separated by hyphens, enclosed in parentheses (00000000-0000-0000-0000-000000000000)(00000000-0000-0000-0000-000000000000)
X 4 番目の値が中かっこで囲まれた 8 つの 16 進値のサブセットをかっこで囲まれた 4 つの 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}}

メソッドをスローするFormatException文字列を正常に解析することがない場合。The method throws a FormatException if it is unable to successfully parse the string. なぜこれが発生する理由のものを次に示します。Here are some of the reasons why this might occur include:

  • input 16 進数の文字セットの一部ではない文字が含まれています。input contains characters that are not part of the hexadecimal character set.

  • input 多すぎるか少なすぎる数字が含まれています。input has too many or too few numeric characters.

  • input 多すぎるか、特定の形式の適切な数値以外の文字が少なすぎます。input has too many or too few of the non-numeric characters appropriate for a particular format.

  • input によって認識される形式のいずれかでは、ToStringメソッドと上記の表に一覧表示します。input is not in one of the formats recognized by the ToString method and listed in the previous table.

使用して、TryParse例外を処理することがなく、解析が失敗した操作をキャッチするメソッド。Use the TryParse method to catch any unsuccessful parse operations without having to handle an exception.

こちらもご覧ください

Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>) Parse(ReadOnlySpan<Char>)

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

パラメーター

戻り値

適用対象