指定したスタイルおよびカルチャ固有の書式の数値の文字列形式を、それと等価の Byte に変換します。
Overloads Public Shared Function Parse( _
ByVal s As String, _ ByVal style As NumberStyles, _ ByVal provider As IFormatProvider _) As Byte
[C#]
public static byte Parse(strings,NumberStylesstyle,IFormatProviderprovider);
[C++]
public: static unsigned char Parse(String* s,NumberStylesstyle,IFormatProvider* provider);
[JScript]
public static function Parse(
s : String,style : NumberStyles,provider : IFormatProvider) : Byte;
パラメータ
- s
変換する数値を格納する文字列。文字列は、 style で指定されたスタイルを使用して解釈されます。 - style
s で使用可能な書式を示す、 NumberStyles 値のビットごとの組み合わせ。 style が null 参照 (Visual Basic では Nothing) の場合、文字列は Integer スタイルを使用して解釈されます。 - provider
s に関するカルチャに固有の書式情報を提供する IFormatProvider 。 provider が null 参照 (Visual Basic では Nothing) の場合は、現在のシステムのカルチャが使用されます。
戻り値
s に指定されている数値と等価の Byte 値。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | s が null 参照 (Visual Basic では Nothing) です。 |
FormatException | s の書式が正しくありません。 |
OverflowException | s が MinValue 未満の数値か、 MaxValue より大きい数値を表しています。 |
ArgumentException | style が、 NumberStyles 値の有効なビットごとの組み合わせではありません。 |
解説
s パラメータは、 provider によって提供される NumberFormatInfo の書式情報を使用して解析されます。
s パラメータには、次の書式の数値を指定します。
[ws][sign]digits[ws]
角かっこ ('[' および ']') で囲まれている項目は省略可能です。その他の項目は次のとおりです。
- ws
省略可能な空白。 - sign
省略可能な正の符号。 - digits
0 から 9 までの一連の数字。
style パラメータは、0 個以上の NumberStyles 値のビットごとの組み合わせです (ただし、このメソッドで返される型としては無効な Any と AllowDecimalPoint は除きます)。
provider パラメータは、 NumberFormatInfo オブジェクトを提供する IFormatProvider インスタンスです。 NumberFormatInfo オブジェクトは、 s の書式に関するカルチャ固有の情報を提供します。
使用例
[Visual Basic, C#, C++] Parse メソッドの複数のオーバーロードを使用して Byte 値の文字列形式を解析する例を次に示します。
' Example of the Byte.Parse( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Module ByteParseDemo
Sub ByteParse( styles As NumberStyles, _
provider As IFormatProvider )
Dim byteFormats As String( ) = { _
" 99 ", " +246 ", " plus246 ", " 1_2_3", " DE " }
' Parse each string in the byteFormat array, using
' NumberStyles and IFormatProvider, if specified.
Dim byteString As String
For Each byteString In byteFormats
Dim byteNumber As Byte
' Display the first part of the output line.
Console.Write( " Parse of {0,-15}", _
String.Format( """{0}""", byteString ) )
' Use the appropriate Byte.Parse overload, based on
' the parameters that are specified.
Try
If provider Is Nothing Then
If styles < 0 Then
byteNumber = Byte.Parse( byteString )
Else
byteNumber = _
Byte.Parse( byteString, styles )
End If
ElseIf styles < 0 Then
byteNumber = _
Byte.Parse( byteString, provider )
Else
byteNumber = _
Byte.Parse( byteString, styles, provider )
End If
' Display the resulting value if Parse succeeded.
Console.WriteLine( "succeeded: {0}", _
byteNumber )
' Display the exception message if Parse failed.
Catch ex As Exception
Console.WriteLine( "failed: {0}", ex.Message )
End Try
Next byteString
End Sub
Sub RunParseDemo( )
' Do not use IFormatProvider or NumberStyles.
Console.WriteLine( vbCrLf & _
"NumberStyles and IFormatProvider are not used:" )
ByteParse( CType( -1, NumberStyles ), Nothing )
' Use NumberStyles.HexNumber; do not use IFormatProvider.
Console.WriteLine( vbCrLf & "NumberStyles." & _
"HexNumber is used; IFormatProvider is not used:" )
ByteParse( NumberStyles.HexNumber, Nothing )
' Get the NumberFormatInfo object from the invariant
' culture, and set the positive-sign string to "plus".
Dim culture As New CultureInfo( "" )
Dim numFormat As NumberFormatInfo = culture.NumberFormat
numFormat.PositiveSign = "plus"
' Use the NumberFormatInfo object as the IFormatProvider.
' Do not use a NumberStyles value.
Console.WriteLine( vbCrLf & "A NumberStyles value " & _
"is not used, but the positive sign is ""plus"":" )
ByteParse( CType( -1, NumberStyles ), numFormat )
' Change the digit group separator to '_' and the
' digit group size to 1.
numFormat.NumberGroupSeparator = "_"
numFormat.NumberGroupSizes = New Integer( ) { 1 }
' Use NumberStyles.Number and the same IFormatProvider.
Console.WriteLine( vbCrLf & "NumberStyles.Number is " & _
"used, group separator = ""_"", size = 1:" )
ByteParse( NumberStyles.Number, numFormat )
End Sub
Sub Main( )
Console.WriteLine( "This example of" & vbCrLf & _
" Byte.Parse( string )," & vbCrLf & _
" Byte.Parse( string, NumberStyles )," & vbCrLf & _
" Byte.Parse( string, IFormatProvider ), and " & _
vbCrLf & " Byte.Parse( string, NumberStyles, " & _
"IFormatProvider )" & vbCrLf & "generates the " & _
"following output when parsing string representations" & _
vbCrLf & "of Byte values with each of these " & _
"forms of Byte.Parse( )." )
RunParseDemo( )
End Sub
End Module
' This example of
' Byte.Parse( string ),
' Byte.Parse( string, NumberStyles ),
' Byte.Parse( string, IFormatProvider ), and
' Byte.Parse( string, NumberStyles, IFormatProvider )
' generates the following output when parsing string representations
' of Byte values with each of these forms of Byte.Parse( ).
'
' NumberStyles and IFormatProvider are not used:
' Parse of " 99 " succeeded: 99
' Parse of " +246 " succeeded: 246
' Parse of " plus246 " failed: Input string was not in a correct format.
' Parse of " 1_2_3" failed: Input string was not in a correct format.
' Parse of " DE " failed: Input string was not in a correct format.
'
' NumberStyles.HexNumber is used; IFormatProvider is not used:
' Parse of " 99 " succeeded: 153
' Parse of " +246 " failed: Input string was not in a correct format.
' Parse of " plus246 " failed: Input string was not in a correct format.
' Parse of " 1_2_3" failed: Input string was not in a correct format.
' Parse of " DE " succeeded: 222
'
' A NumberStyles value is not used, but the positive sign is "plus":
' Parse of " 99 " succeeded: 99
' Parse of " +246 " failed: Input string was not in a correct format.
' Parse of " plus246 " succeeded: 246
' Parse of " 1_2_3" failed: Input string was not in a correct format.
' Parse of " DE " failed: Input string was not in a correct format.
'
' NumberStyles.Number is used, group separator = "_", size = 1:
' Parse of " 99 " succeeded: 99
' Parse of " +246 " failed: Input string was not in a correct format.
' Parse of " plus246 " succeeded: 246
' Parse of " 1_2_3" succeeded: 123
' Parse of " DE " failed: Input string was not in a correct format.
[C#]
// Example of the Byte.Parse( ) methods.
using System;
using System.Globalization;
class ByteParseDemo
{
static void ByteParse( NumberStyles styles,
IFormatProvider provider )
{
string[ ] byteFormats = {
" 99 ", " +246 ", " plus246 ", " 1_2_3", " DE " };
// Parse each string in the byteFormat array, using
// NumberStyles and IFormatProvider, if specified.
foreach ( string byteString in byteFormats )
{
byte byteNumber;
// Display the first part of the output line.
Console.Write( " Parse of {0,-15}",
String.Format( "\"{0}\"", byteString ) );
// Use the appropriate Byte.Parse overload, based
// on the parameters that are specified.
try
{
if( provider == null )
if( styles < 0 )
byteNumber = Byte.Parse( byteString );
else
byteNumber =
Byte.Parse( byteString, styles );
else if( styles < 0 )
byteNumber =
Byte.Parse( byteString, provider );
else
byteNumber =
Byte.Parse( byteString, styles, provider );
// Display the resulting value if Parse succeeded.
Console.WriteLine( "succeeded: {0}", byteNumber );
}
catch( Exception ex )
{
// Display the exception message if Parse failed.
Console.WriteLine( "failed: {0}", ex.Message );
}
}
}
static void RunParseDemo( )
{
// Do not use IFormatProvider or NumberStyles.
Console.WriteLine(
"\nNumberStyles and IFormatProvider are not used:" );
ByteParse( (NumberStyles)(-1), null );
// Use NumberStyles.HexNumber; do not use IFormatProvider.
Console.WriteLine( "\nNumberStyles.HexNumber, " +
"is used; IFormatProvider is not used:" );
ByteParse( NumberStyles.HexNumber, null );
// Get the NumberFormatInfo object from the invariant
// culture, and set the positive-sign string to "plus".
CultureInfo culture = new CultureInfo( "" );
NumberFormatInfo numFormat = culture.NumberFormat;
numFormat.PositiveSign = "plus";
// Use the NumberFormatInfo object as the IFormatProvider.
// Do not use a NumberStyles value.
Console.WriteLine( "\nA NumberStyles value is not used, " +
"but the positive sign is \"plus\":" );
ByteParse( (NumberStyles)(-1), numFormat );
// Change the digit group separator to '_' and the
// digit group size to 1.
numFormat.NumberGroupSeparator = "_";
numFormat.NumberGroupSizes = new int[ ] { 1 };
// Use NumberStyles.Number and the same IFormatProvider.
Console.WriteLine( "\nNumberStyles.Number is " +
"used, group separator = \"_\", size = 1:" );
ByteParse( NumberStyles.Number, numFormat );
}
static void Main( )
{
Console.WriteLine( "This example of\n" +
" Byte.Parse( string ),\n" +
" Byte.Parse( string, NumberStyles ),\n" +
" Byte.Parse( string, IFormatProvider ), and \n" +
" Byte.Parse( string, NumberStyles, IFormatProvider" +
" )\ngenerates the following output when parsing " +
"string representations\nof Byte values with each " +
"of these forms of Byte.Parse( )." );
RunParseDemo( );
}
}
/*
This example of
Byte.Parse( string ),
Byte.Parse( string, NumberStyles ),
Byte.Parse( string, IFormatProvider ), and
Byte.Parse( string, NumberStyles, IFormatProvider )
generates the following output when parsing string representations
of Byte values with each of these forms of Byte.Parse( ).
NumberStyles and IFormatProvider are not used:
Parse of " 99 " succeeded: 99
Parse of " +246 " succeeded: 246
Parse of " plus246 " failed: Input string was not in a correct format.
Parse of " 1_2_3" failed: Input string was not in a correct format.
Parse of " DE " failed: Input string was not in a correct format.
NumberStyles.HexNumber, is used; IFormatProvider is not used:
Parse of " 99 " succeeded: 153
Parse of " +246 " failed: Input string was not in a correct format.
Parse of " plus246 " failed: Input string was not in a correct format.
Parse of " 1_2_3" failed: Input string was not in a correct format.
Parse of " DE " succeeded: 222
A NumberStyles value is not used, but the positive sign is "plus":
Parse of " 99 " succeeded: 99
Parse of " +246 " failed: Input string was not in a correct format.
Parse of " plus246 " succeeded: 246
Parse of " 1_2_3" failed: Input string was not in a correct format.
Parse of " DE " failed: Input string was not in a correct format.
NumberStyles.Number is used, group separator = "_", size = 1:
Parse of " 99 " succeeded: 99
Parse of " +246 " failed: Input string was not in a correct format.
Parse of " plus246 " succeeded: 246
Parse of " 1_2_3" succeeded: 123
Parse of " DE " failed: Input string was not in a correct format.
*/
[C++]
// Example of the Byte::Parse( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;
void ByteParse( NumberStyles styles, IFormatProvider* provider )
{
String* byteFormats [ ] = __gc new String*[ 5 ];
byteFormats[ 0 ] = S" 99 ";
byteFormats[ 1 ] = S" +246 ";
byteFormats[ 2 ] = S" plus246 ";
byteFormats[ 3 ] = S" 1_2_3";
byteFormats[ 4 ] = S" DE ";
// Parse each string in the byteFormat array, using
// NumberStyles and IFormatProvider, if specified.
// This implements foreach( String* byteString in byteFormats ).
IEnumerator* myEnum = byteFormats->GetEnumerator( );
while( myEnum->MoveNext( ) )
{
String* byteString = __try_cast<String*>( myEnum->Current );
Byte byteNumber;
// Display the first part of the output line.
Console::Write( S" Parse of {0,-15}",
String::Format( S"\"{0}\"", byteString ) );
try
{
// Use the appropriate Byte::Parse overload, based
// on the parameters that are specified.
if( provider == (IFormatProvider*)0 )
if( styles < 0 )
byteNumber = Byte::Parse( byteString );
else
byteNumber = Byte::Parse( byteString, styles );
else if( styles < 0 )
byteNumber = Byte::Parse( byteString, provider );
else
byteNumber =
Byte::Parse( byteString, styles, provider );
// Display the resulting value if Parse succeeded.
Console::WriteLine( S"succeeded: {0}",
__box( byteNumber ) );
}
catch( Exception* ex )
{
// Display the exception message if Parse failed.
Console::WriteLine( S"failed: {0}", ex->Message );
}
}
}
void RunParseDemo( )
{
IFormatProvider* null = 0;
// Do not use IFormatProvider or NumberStyles.
Console::WriteLine(
S"\nNumberStyles and IFormatProvider are not used:" );
ByteParse( (NumberStyles)(-1), null );
// Use NumberStyles.HexNumber; do not use IFormatProvider.
Console::WriteLine( S"\nNumberStyles::HexNumber "
S"is used; IFormatProvider is not used:" );
ByteParse( NumberStyles::HexNumber, null );
// Get the NumberFormatInfo object from the invariant
// culture, and set the positive-sign string to "plus".
CultureInfo* culture = new CultureInfo( S"" );
NumberFormatInfo* numFormat = culture->NumberFormat;
numFormat->PositiveSign = S"plus";
// Use the NumberFormatInfo object as the IFormatProvider.
// Do not use a NumberStyles value.
Console::WriteLine( S"\nA NumberStyles value is not used, "
S"but the positive sign is \"plus\":" );
ByteParse( (NumberStyles)(-1), numFormat );
// Change the digit group separator to '_' and the
// digit group size to 1.
numFormat->NumberGroupSeparator = S"_";
Int32 sizes __gc [ ] = { 1 };
numFormat->NumberGroupSizes = sizes;
// Use NumberStyles::Number and the same IFormatProvider.
Console::WriteLine( S"\nNumberStyles.Number is "
S"used, group separator = \"_\", size = 1:" );
ByteParse( NumberStyles::Number, numFormat );
}
void main( )
{
Console::WriteLine( S"This example of\n"
S" Byte::Parse( String* ),\n"
S" Byte::Parse( String*, NumberStyles ),\n"
S" Byte::Parse( String*, IFormatProvider* ), and \n"
S" Byte::Parse( String*, NumberStyles, IFormatProvider*"
S" )\ngenerates the following output when parsing "
S"string representations\nof Byte values with each "
S"of these forms of Byte::Parse( )." );
RunParseDemo( );
}
/*
This example of
Byte::Parse( String* ),
Byte::Parse( String*, NumberStyles ),
Byte::Parse( String*, IFormatProvider* ), and
Byte::Parse( String*, NumberStyles, IFormatProvider* )
generates the following output when parsing string representations
of Byte values with each of these forms of Byte::Parse( ).
NumberStyles and IFormatProvider are not used:
Parse of " 99 " succeeded: 99
Parse of " +246 " succeeded: 246
Parse of " plus246 " failed: Input string was not in a correct format.
Parse of " 1_2_3" failed: Input string was not in a correct format.
Parse of " DE " failed: Input string was not in a correct format.
NumberStyles::HexNumber is used; IFormatProvider is not used:
Parse of " 99 " succeeded: 153
Parse of " +246 " failed: Input string was not in a correct format.
Parse of " plus246 " failed: Input string was not in a correct format.
Parse of " 1_2_3" failed: Input string was not in a correct format.
Parse of " DE " succeeded: 222
A NumberStyles value is not used, but the positive sign is "plus":
Parse of " 99 " succeeded: 99
Parse of " +246 " failed: Input string was not in a correct format.
Parse of " plus246 " succeeded: 246
Parse of " 1_2_3" failed: Input string was not in a correct format.
Parse of " DE " failed: Input string was not in a correct format.
NumberStyles.Number is used, group separator = "_", size = 1:
Parse of " 99 " succeeded: 99
Parse of " +246 " failed: Input string was not in a correct format.
Parse of " plus246 " succeeded: 246
Parse of " 1_2_3" succeeded: 123
Parse of " DE " failed: Input string was not in a correct format.
*/
[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
参照
Byte 構造体 | Byte メンバ | System 名前空間 | Byte.Parse オーバーロードの一覧 | 書式設定の概要 | ToString | MaxValue | MinValue | NumberStyles | NumberFormatInfo | IFormatProvider