次の方法で共有


Convert.ToChar メソッド (String, IFormatProvider)

指定したカルチャに固有の書式情報を使用して、 String の 1 番目の文字を Unicode 文字に変換します。

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

パラメータ

  • value
    長さ 1 の String または null 参照 (Visual Basic では Nothing) 。
  • provider
    (予約済み) カルチャに固有の書式情報を提供する IFormatProvider インターフェイス実装。

戻り値

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

例外

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

解説

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

provider は無視され、この操作には関与しません。

使用例

[Visual Basic, C#, C++] 目的の書式プロバイダの型を表示する IFormatProvider オブジェクトを使用し、 String 形式の Char の値を ToChar メソッドで変換するコード例を次に示します。書式プロバイダが参照されないことを示す例です。

 
' Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class DummyProvider
    Implements IFormatProvider

    ' Normally, GetFormat returns an object of the requested type
    ' (usually itself) if it is able; otherwise, it returns Nothing. 
    Public Function GetFormat( argType As Type ) As Object _
        Implements IFormatProvider.GetFormat

        ' Here, GetFormat displays the name of argType, after removing 
        ' the namespace information. GetFormat always returns Nothing.
        Dim argStr  As String = argType.ToString( )
        If argStr = "" Then argStr = "Empty"
        argStr = argStr.Substring( argStr.LastIndexOf( "."c ) + 1 )

        Console.Write( "{0,-20}", argStr )
        Return Nothing

    End Function 
End Class

Module ConvertNonNumericProviderDemo

    Sub Main( )

        ' Create an instance of IFormatProvider.
        Dim provider    As New DummyProvider( )
        Dim format      As String   = "{0,-17}{1,-17}{2}"

        ' Convert these values using DummyProvider.
        Dim Int32A      As String   = "-252645135"   
        Dim DoubleA     As String   = "61680.3855"
        Dim DayTimeA    As String   = "2001/9/11 13:45"

        Dim BoolA       As String   = "True"
        Dim StringA     As String   = "Qwerty"
        Dim CharA       As String   = "$"

        Console.WriteLine( "This example of selected " & _
            "Convert.ToXXX( String, IFormatProvider ) " & vbCrLf & _
            "methods generates the following output. The example " & _
            "displays the " & vbCrLf & "provider type if the " & _
            "IFormatProvider is called." )
        Console.WriteLine( vbCrLf & _
            "Note: For the ToBoolean, ToString, and ToChar " & _
            "methods, the " & vbCrLf & "IFormatProvider object " & _
            "is not referenced." )

        ' The format provider is called for the following conversions.
        Console.WriteLine( )
        Console.WriteLine( format, "ToInt32", Int32A, _
            Convert.ToInt32( Int32A, provider ) )
        Console.WriteLine( format, "ToDouble", DoubleA, _
            Convert.ToDouble( DoubleA, provider ) )
        Console.WriteLine( format, "ToDateTime", DayTimeA, _
            Convert.ToDateTime( DayTimeA, provider ) )

        ' The format provider is not called for these conversions.
        Console.WriteLine( )
        Console.WriteLine( format, "ToBoolean", BoolA, _
            Convert.ToBoolean( BoolA, provider ) )
        Console.WriteLine( format, "ToString", StringA, _
            Convert.ToString( StringA, provider ) )
        Console.WriteLine( format, "ToChar", CharA, _
            Convert.ToChar( CharA, provider ) )

    End Sub
End Module

' This example of selected Convert.ToXXX( String, IFormatProvider )
' methods generates the following output. The example displays the
' provider type if the IFormatProvider is called.
'
' Note: For the ToBoolean, ToString, and ToChar methods, the
' IFormatProvider object is not referenced.
' 
' NumberFormatInfo    ToInt32          -252645135       -252645135
' NumberFormatInfo    ToDouble         61680.3855       61680.3855
' DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM
' 
' ToBoolean        True             True
' ToString         Qwerty           Qwerty
' ToChar           $                $

[C#] 
// Example of selected Convert.ToXXX( String, IFormatProvider ) methods.
using System;
using System.Globalization;

public class DummyProvider : IFormatProvider
{
    // Normally, GetFormat returns an object of the requested type
    // (usually itself) if it is able; otherwise, it returns Nothing. 
    public object GetFormat(Type argType)
    {
        // Here, GetFormat displays the name of argType, after removing 
        // the namespace information. GetFormat always returns null.
        string argStr = argType.ToString( );
        if( argStr == "" ) 
            argStr = "Empty";
        argStr = argStr.Substring( argStr.LastIndexOf( '.' ) + 1 );

        Console.Write( "{0,-20}", argStr );
        return null;
    }
}

class ConvertNonNumericProviderDemo
{
    public static void Main( )
    {
        // Create an instance of IFormatProvider.
        DummyProvider provider = new DummyProvider( );
        string format   = "{0,-17}{1,-17}{2}";

        // Convert these values using DummyProvider.
        string Int32A   = "-252645135";
        string DoubleA  = "61680.3855";
        string DayTimeA = "2001/9/11 13:45";

        string BoolA    = "True";
        string StringA  = "Qwerty";
        string CharA    = "$";

        Console.WriteLine( "This example of selected " +
            "Convert.ToXXX( String, IFormatProvider ) \nmethods " +
            "generates the following output. The example displays " +
            "the \nprovider type if the IFormatProvider is called." );
        Console.WriteLine( "\nNote: For the " +
            "ToBoolean, ToString, and ToChar methods, the \n" +
            "IFormatProvider object is not referenced." );

        // The format provider is called for the following conversions.
        Console.WriteLine( );
        Console.WriteLine( format, "ToInt32", Int32A, 
            Convert.ToInt32( Int32A, provider ) );
        Console.WriteLine( format, "ToDouble", DoubleA, 
            Convert.ToDouble( DoubleA, provider ) );
        Console.WriteLine( format, "ToDateTime", DayTimeA, 
            Convert.ToDateTime( DayTimeA, provider ) );

        // The format provider is not called for these conversions.
        Console.WriteLine( );
        Console.WriteLine( format, "ToBoolean", BoolA, 
            Convert.ToBoolean( BoolA, provider ) );
        Console.WriteLine( format, "ToString", StringA, 
            Convert.ToString( StringA, provider ) );
        Console.WriteLine( format, "ToChar", CharA, 
            Convert.ToChar( CharA, provider ) );
    }
}

/*
This example of selected Convert.ToXXX( String, IFormatProvider )
methods generates the following output. The example displays the
provider type if the IFormatProvider is called.

Note: For the ToBoolean, ToString, and ToChar methods, the
IFormatProvider object is not referenced.

NumberFormatInfo    ToInt32          -252645135       -252645135
NumberFormatInfo    ToDouble         61680.3855       61680.3855
DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM

ToBoolean        True             True
ToString         Qwerty           Qwerty
ToChar           $                $
*/ 

[C++] 
// Example of selected Convert::ToXXX( String*, IFormatProvider* ) 
// methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

__gc class DummyProvider : public IFormatProvider
{
    // Normally, GetFormat returns an object of the requested type
    // (usually itself) if it is able; otherwise, it returns Nothing. 
public: Object* GetFormat( Type* argType )
    {
        // Here, GetFormat displays the name of argType, after removing 
        // the namespace information. GetFormat always returns null.
        String* argStr = argType->ToString( );
        if( argStr == S"" ) 
            argStr = S"Empty";
        argStr = argStr->Substring( argStr->LastIndexOf( '.' ) + 1 );

        Console::Write( S"{0,-20}", argStr );
        return (Object*)0;
    }
};

void main( )
{
    // Create an instance of IFormatProvider.
    DummyProvider* provider = new DummyProvider( );
    String* format   = S"{0,-17}{1,-17}{2}";

    // Convert these values using DummyProvider.
    String* Int32A   = S"-252645135";
    String* DoubleA  = S"61680.3855";
    String* DayTimeA = S"2001/9/11 13:45";

    String* BoolA    = S"True";
    String* StringA  = S"Qwerty";
    String* CharA    = S"$";

    Console::WriteLine( S"This example of selected " 
        S"Convert::ToXXX( String*, IFormatProvider* ) \nmethods " 
        S"generates the following output. The example displays the "
        S"\nprovider type if the IFormatProvider is called." );
    Console::WriteLine( S"\nNote: For the " 
        S"ToBoolean, ToString, and ToChar methods, the \n" 
        S"IFormatProvider object is not referenced." );

    // The format provider is called for the following conversions.
    Console::WriteLine( );
    Console::WriteLine( format, S"ToInt32", Int32A, 
        __box( Convert::ToInt32( Int32A, provider ) ) );
    Console::WriteLine( format, S"ToDouble", DoubleA, 
        __box( Convert::ToDouble( DoubleA, provider ) ) );
    Console::WriteLine( format, S"ToDateTime", DayTimeA, 
        __box( Convert::ToDateTime( DayTimeA, provider ) ) );

    // The format provider is not called for these conversions.
    Console::WriteLine( );
    Console::WriteLine( format, S"ToBoolean", BoolA, 
        __box( Convert::ToBoolean( BoolA, provider ) ) );
    Console::WriteLine( format, S"ToString", StringA, 
        Convert::ToString( StringA, provider ) );
    Console::WriteLine( format, S"ToChar", CharA, 
        __box( Convert::ToChar( CharA, provider ) ) );
}

/*
This example of selected Convert::ToXXX( String*, IFormatProvider* )
methods generates the following output. The example displays the
provider type if the IFormatProvider is called.

Note: For the ToBoolean, ToString, and ToChar methods, the
IFormatProvider object is not referenced.

NumberFormatInfo    ToInt32          -252645135       -252645135
NumberFormatInfo    ToDouble         61680.3855       61680.3855
DateTimeFormatInfo  ToDateTime       2001/9/11 13:45  9/11/2001 1:45:00 PM

ToBoolean        True             True
ToString         Qwerty           Qwerty
ToChar           $                $
*/ 

[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

参照

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