次の方法で共有


Convert.ToChar メソッド (String)

String の 1 番目の文字を Unicode 文字に変換します。

Overloads Public Shared Function ToChar( _
   ByVal value As String _) As Char
[C#]
public static char ToChar(stringvalue);
[C++]
public: static __wchar_t ToChar(String* value);
[JScript]
public static function ToChar(
   value : String) : Char;

パラメータ

  • value
    長さ 1 の String または null 参照 (Visual Basic では Nothing) 。

戻り値

value の唯一の文字、つまり 1 番目の文字と等価の Unicode 文字。

例外

例外の種類 条件
ArgumentNullException value が null 参照 (Visual Basic では Nothing) です。
FormatException value の長さが 1 ではありません。

解説

value は、 null 参照 (Visual Basic では Nothing) または 1 文字だけが格納された String である必要があります。

使用例

ToByte を使用して、 String の値を Char に変換する方法については、次のコード例を参照してください。

 
Public Sub ConvertStringChar(ByVal stringVal As String)
    Dim charVal As Char = "a"c

    ' A string must be one character long to convert to char.
    Try
        charVal = System.Convert.ToChar(stringVal)
        System.Console.WriteLine("{0} as a char is {1}", _
                                  stringVal, charVal)
    Catch exception As System.FormatException
        System.Console.WriteLine( _
         "The string is longer than one character.")
    Catch exception As System.ArgumentNullException
        System.Console.WriteLine("The string is null.")
    End Try

    ' A char to string conversion will always succeed.
    stringVal = System.Convert.ToString(charVal)
    System.Console.WriteLine("The character as a string is {0}", _
                              stringVal)
End Sub

[C#] 
public void ConvertStringChar(string stringVal) {
    char charVal = 'a';

    // A string must be one character long to convert to char.
    try {
        charVal = System.Convert.ToChar(stringVal);
        System.Console.WriteLine("{0} as a char is {1}",
            stringVal, charVal);
    }
    catch (System.FormatException) {
        System.Console.WriteLine(
            "The string is longer than one character.");
    }
    catch (System.ArgumentNullException) {
        System.Console.WriteLine("The string is null.");
    }

    // A char to string conversion will always succeed.
    stringVal = System.Convert.ToString(charVal);
    System.Console.WriteLine("The character as a string is {0}",
            stringVal);
}

[C++] 

   void ConvertStringChar(String* stringVal)
   {
      Char charVal = 'a';

      // A String* must be one character long to convert to char.
      try {
         charVal = System::Convert::ToChar(stringVal);
         System::Console::WriteLine(S" {0} as a char is {1}",
            stringVal, __box(charVal));
      } catch (System::FormatException*) {
         System::Console::WriteLine(S"The String* is longer than one character.");
      } catch (System::ArgumentNullException*) {
         System::Console::WriteLine(S"The String* is 0.");
      }

      // A char to String* conversion will always succeed.
      stringVal = System::Convert::ToString(charVal);
      System::Console::WriteLine(S"The character as a String* is {0}",
         stringVal);
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Convert クラス | Convert メンバ | System 名前空間 | Convert.ToChar オーバーロードの一覧