Byte.TryParse 方法

定义

尝试将数字的字符串表示形式转换为它的等效 Byte,并返回一个指示转换是否成功的值。

重载

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

尝试将 UTF-8 字符范围解析为值。

TryParse(ReadOnlySpan<Char>, Byte)

尝试将数字的范围表示形式转换为它的等效 Byte,并返回一个指示转换是否成功的值。

TryParse(String, Byte)

尝试将数字的字符串表示形式转换为它的等效 Byte,并返回一个指示转换是否成功的值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

尝试将字符范围解析为值。

TryParse(String, IFormatProvider, Byte)

尝试将字符串解析为值。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte)

尝试将 UTF-8 字符范围解析为值。

TryParse(ReadOnlySpan<Byte>, Byte)

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为等效的 8 位无符号整数。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte)

将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 Byte。 一个指示转换是否成功的返回值。

TryParse(String, NumberStyles, IFormatProvider, Byte)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 Byte。 一个指示转换是否成功的返回值。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs

尝试将 UTF-8 字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = IUtf8SpanParsable<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Byte) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符的跨度。

provider
IFormatProvider

一个对象,提供有关 utf8Text 的区域性特定格式设置信息。

result
Byte

返回时,包含成功分析 utf8Text 的结果或失败时的未定义值。

返回

true 如果 utf8Text 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Char>, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

尝试将数字的范围表示形式转换为它的等效 Byte,并返回一个指示转换是否成功的值。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse (ReadOnlySpan<char> s, out byte result);
static member TryParse : ReadOnlySpan<char> * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Byte) As Boolean

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的数字的字符。

result
Byte

当此方法返回时,如果转换成功,则包含与 Byte 中所包含的数字等效的 s 值;如果转换失败,则包含零。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

适用于

TryParse(String, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

尝试将数字的字符串表示形式转换为它的等效 Byte,并返回一个指示转换是否成功的值。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse (string s, out byte result);
public static bool TryParse (string? s, out byte result);
static member TryParse : string * byte -> bool
Public Shared Function TryParse (s As String, ByRef result As Byte) As Boolean

参数

s
String

包含要转换的数字的字符串。

result
Byte

当此方法返回时,如果转换成功,则包含与 Byte 中所包含的数字等效的 s 值;如果转换失败,则包含零。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

示例

以下示例使用许多不同的字符串值调用 TryParse(String, Byte) 方法。

using namespace System;

void main()
{
   array<String^>^ byteStrings = gcnew array<String^> { nullptr, String::Empty, 
                                                        "1024", "100.1", "100", 
                                                        "+100", "-100", "000000000000000100", 
                                                        "00,100", "   20   ", "FF", "0x1F" };
   Byte byteValue;
   for each (String^ byteString in byteStrings) {
      bool result = Byte::TryParse(byteString, byteValue);
      if (result)
         Console::WriteLine("Converted '{0}' to {1}", 
                            byteString, byteValue);
      else
         Console::WriteLine("Attempted conversion of '{0}' failed.", 
                            byteString);
   }
}
// The example displays the following output:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.`
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.}
using System;

public class ByteConversion
{
   public static void Main()
   {
      string[] byteStrings = { null, string.Empty, "1024",
                               "100.1", "100", "+100", "-100",
                               "000000000000000100", "00,100",
                               "   20   ", "FF", "0x1F" };

      foreach (var byteString in byteStrings)
      {
          CallTryParse(byteString);
      }
   }

   private static void CallTryParse(string stringToConvert)
   {
      byte byteValue;
      bool success = Byte.TryParse(stringToConvert, out byteValue);
      if (success)
      {
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      }
      else
      {
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert);
      }
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
open System

let callTryParse (stringToConvert: string) =
    match Byte.TryParse stringToConvert with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."

let byteStrings = 
    [ null; String.Empty; "1024"
      "100.1"; "100"; "+100"; "-100"
      "000000000000000100"; "00,100"
      "   20   "; "FF"; "0x1F" ]

for byteString in byteStrings do
    callTryParse byteString

// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
Module ByteConversion
   Public Sub Main()
      Dim byteStrings() As String = { Nothing, String.Empty, "1024", 
                                    "100.1", "100", "+100", "-100",
                                    "000000000000000100", "00,100",
                                    "   20   ", "FF", "0x1F"}

      For Each byteString As String In byteStrings
        CallTryParse(byteString)
      Next
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String)  
      Dim byteValue As Byte
      Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
      If success Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert)
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '' failed.
'       Attempted conversion of '' failed.
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Attempted conversion of '00,100' failed.
'       Converted '   20   ' to 20
'       Attempted conversion of 'FF' failed.
'       Attempted conversion of '0x1F' failed.

注解

如果 s 参数的格式不正确,则转换失败,如果 参数为 nullString.Empty,或者如果它表示小于MinValue或大于 MaxValue的数字,则方法将返回 false

方法 Byte.TryParse(String, Byte) 类似于 方法 Byte.Parse(String) ,只是在 TryParse(String, Byte) 转换失败时不会引发异常。

参数 s 应是数字的字符串表示形式,形式如下:

[ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。
sign 一个可选的正符号,由 NumberFormatInfo.PositiveSign 当前区域性的 属性指定。
位数 范围从 0 到 9 的十进制数字序列。

参数 s 使用 Integer 样式进行解释。 除了字节值的十进制数字之外,只允许使用前导空格和尾随空格以及前导符号。 (如果符号存在,则它必须是正号,否则该方法将 OverflowException引发 .) 若要显式定义样式元素以及中 s存在的特定于区域性的格式设置信息,请使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

参数 s 是使用对象中 NumberFormatInfo 当前区域性的格式设置信息进行分析的。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo

方法的 Byte.TryParse(String, Byte) 此重载将 参数中的所有 s 数字解释为十进制数字。 若要分析十六进制数的字符串表示形式,请调用 Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) 重载。

另请参阅

适用于

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

尝试将字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = ISpanParsable<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Byte) As Boolean

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Byte

此方法返回时,包含成功分析 s的结果或失败时的未定义值。

返回

true 如果 s 已成功分析,则为 ;否则为 false

适用于

TryParse(String, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

尝试将字符串解析为值。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = IParsable<System::Byte>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out byte result);
static member TryParse : string * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Byte) As Boolean

参数

s
String

要分析的字符串。

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。

result
Byte

此方法返回时,包含成功分析 s 的结果或失败时的未定义值。

返回

true 如果 s 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs

尝试将 UTF-8 字符范围解析为值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符的跨度。

style
NumberStyles

可以存在于 中的 utf8Text数字样式的按位组合。

provider
IFormatProvider

一个对象,提供有关 utf8Text 的区域性特定格式设置信息。

result
Byte

返回时,包含成功分析 utf8Text 的结果或失败时的未定义值。

返回

true 如果 utf8Text 已成功分析,则为 ;否则为 false

适用于

TryParse(ReadOnlySpan<Byte>, Byte)

Source:
Byte.cs
Source:
Byte.cs

尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为等效的 8 位无符号整数。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out byte result);
static member TryParse : ReadOnlySpan<byte> * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Byte) As Boolean

参数

utf8Text
ReadOnlySpan<Byte>

包含表示要转换的数字的 UTF-8 字符的跨度。

result
Byte

当此方法返回时,如果转换成功,则包含与 utf8Text 中所含数字等效的 8 位无符号整数值;如果转换失败,则包含零。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 utf8Text;否则为 false

适用于

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 Byte。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result);
public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的数字的字符。 该范围使用 Integer 样式来进行解释。

style
NumberStyles

枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 Integer

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。 如果 providernull,则使用当前区域性。

result
Byte

当此方法返回时,如果转换成功,则包含与 s 中所含数字等效的 8 位无符号整数值;如果转换失败,则包含零。 如果 s 参数为 nullEmpty,格式不正确,或表示小于 Byte.MinValue 或大于 Byte.MaxValue 的数字,则转换失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

适用于

TryParse(String, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 Byte。 一个指示转换是否成功的返回值。

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result);
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean

参数

s
String

包含要转换的数字的字符串。 该字符串使用由 style 指定的样式来进行解释。

style
NumberStyles

枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 Integer

provider
IFormatProvider

一个对象,提供有关 s 的区域性特定格式设置信息。 如果 providernull,则使用当前区域性。

result
Byte

当此方法返回时,如果转换成功,则包含与 s 中所含数字等效的 8 位无符号整数值;如果转换失败,则包含零。 如果 s 参数为 nullEmpty,格式不正确,或者表示小于 Byte.MinValue 或大于 Byte.MaxValue 的数字,则转换失败。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回

如果 true 成功转换,则为 s;否则为 false

例外

style 不是 NumberStyles 值。

- 或 -

style 不是 AllowHexSpecifierHexNumber 值的组合。

示例

以下示例使用许多不同的字符串值调用 TryParse(String, NumberStyles, IFormatProvider, Byte) 方法。

using namespace System;
using namespace System::Globalization;

void CallTryParse(String^ byteString, NumberStyles styles);

void main()
{
   String^ byteString; 
   NumberStyles styles;

   byteString = "1024";
   styles = NumberStyles::Integer;
   CallTryParse(byteString, styles);

   byteString = "100.1";
   styles = NumberStyles::Integer | NumberStyles::AllowDecimalPoint;
   CallTryParse(byteString, styles);

   byteString = "100.0";
   CallTryParse(byteString, styles);

   byteString = "+100";
   styles = NumberStyles::Integer | NumberStyles::AllowLeadingSign 
            | NumberStyles::AllowTrailingSign;
   CallTryParse(byteString, styles);

   byteString = "-100";
   CallTryParse(byteString, styles);

   byteString = "000000000000000100";
   CallTryParse(byteString, styles);

   byteString = "00,100";
   styles = NumberStyles::Integer | NumberStyles::AllowThousands;
   CallTryParse(byteString, styles);

   byteString = "2E+3   ";
   styles = NumberStyles::Integer | NumberStyles::AllowExponent;
   CallTryParse(byteString, styles);

   byteString = "FF";
   styles = NumberStyles::HexNumber;
   CallTryParse(byteString, styles);

   byteString = "0x1F";
   CallTryParse(byteString, styles);
}

void CallTryParse(String^ stringToConvert, NumberStyles styles)
{  
   Byte byteValue;
   bool result = Byte::TryParse(stringToConvert, styles, 
                                 (IFormatProvider^) nullptr , byteValue);
   if (result)
      Console::WriteLine("Converted '{0}' to {1}", 
                     stringToConvert, byteValue);
   else
      Console::WriteLine("Attempted conversion of '{0}' failed.", 
                        stringToConvert);
}
// The example displays the following output:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.}
using System;
using System.Globalization;

public class ByteConversion2
{
   public static void Main()
   {
      string byteString;
      NumberStyles styles;

      byteString = "1024";
      styles = NumberStyles.Integer;
      CallTryParse(byteString, styles);

      byteString = "100.1";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(byteString, styles);

      byteString = "100.0";
      CallTryParse(byteString, styles);

      byteString = "+100";
      styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign
               | NumberStyles.AllowTrailingSign;
      CallTryParse(byteString, styles);

      byteString = "-100";
      CallTryParse(byteString, styles);

      byteString = "000000000000000100";
      CallTryParse(byteString, styles);

      byteString = "00,100";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(byteString, styles);

      byteString = "2E+3   ";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(byteString, styles);

      byteString = "FF";
      styles = NumberStyles.HexNumber;
      CallTryParse(byteString, styles);

      byteString = "0x1F";
      CallTryParse(byteString, styles);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      Byte byteValue;
      bool result = Byte.TryParse(stringToConvert, styles,
                                  null as IFormatProvider, out byteValue);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert.ToString());
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
    match Byte.TryParse(stringToConvert, styles, null) with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."
                        
[<EntryPoint>]
let main _ =
    let byteString = "1024"
    let styles = NumberStyles.Integer
    callTryParse byteString styles

    let byteString = "100.1"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse byteString styles

    let byteString = "100.0"
    callTryParse byteString styles

    let byteString = "+100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
    callTryParse byteString styles

    let byteString = "-100"
    callTryParse byteString styles

    let byteString = "000000000000000100"
    callTryParse byteString styles

    let byteString = "00,100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
    callTryParse byteString styles

    let byteString = "2E+3   "
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse byteString styles

    let byteString = "FF"
    let styles = NumberStyles.HexNumber
    callTryParse byteString styles

    let byteString = "0x1F"
    callTryParse byteString styles

    0

// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
Imports System.Globalization

Module ByteConversion2
   Public Sub Main()
      Dim byteString As String 
      Dim styles As NumberStyles
      
      byteString = "1024"
      styles = NumberStyles.Integer
      CallTryParse(byteString, styles)
      
      byteString = "100.1"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(byteString, styles)
      
      byteString = "100.0"
      CallTryParse(byteString, styles)
      
      byteString = "+100"
      styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
               Or NumberStyles.AllowTrailingSign
      CallTryParse(byteString, styles)
      
      byteString = "-100"
      CallTryParse(byteString, styles)
      
      byteString = "000000000000000100"
      CallTryParse(byteString, styles)
      
      byteString = "00,100"      
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(byteString, styles)
      
      byteString = "2E+3   "
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(byteString, styles)
      
      byteString = "FF"
      styles = NumberStyles.HexNumber
      CallTryParse(byteString, styles)
      
      byteString = "0x1F"
      CallTryParse(byteString, styles)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)  
      Dim byteValue As Byte
      Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
                                            byteValue)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         If stringToConvert Is Nothing Then stringToConvert = ""
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert.ToString())
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100.0' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Converted '00,100' to 100
'       Attempted conversion of '2E+3   ' failed.
'       Converted 'FF' to 255
'       Attempted conversion of '0x1F' failed.

注解

方法 TryParse 与 方法类似 Parse ,只是 TryParse 方法不会在转换失败时引发异常。

参数 s 是使用 参数提供的 对象中的 NumberFormatInfo 格式设置信息进行分析的 provider

style 参数定义 (的样式元素,如空白或正号) ,这些元素在 参数中 s 允许分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 根据 的值 styles 参数可能包括以下元素:

[ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

或者,如果 style 参数包含 AllowHexSpecifier

[ws]hexdigits[ws]

方括号中的元素 ( [ 和 ] ) 是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果包含 NumberStyles.AllowLeadingWhite 标志,则的开头s会出现空格;如果 style style 包含 NumberStyles.AllowTrailingWhite 标志,则可以在 的末尾显示空格。
$ 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyPositivePattern 参数的 方法provider返回GetFormatNumberFormatInfo 对象的 属性定义。 如果style包含 标志,NumberStyles.AllowCurrencySymbol则可以在 中s显示货币符号。
sign 可选的正号。 (如果 .) 中存在s负号,则分析操作会失败。如果包含 NumberStyles.AllowLeadingSign 标志,则符号可以出现在 的s开头;如果stylestyle包含 NumberStyles.AllowTrailingSign 标志,则显示在 末尾s
位数 从 0 到 9 的数字序列。
. 区域性特定的小数点符号。 如果包含 标志,则 指定的provider区域性的小数点符号可以出现在 中sNumberStyles.AllowDecimalPointstyle
fractional_digits 出现一个或多个数字 0。 仅当包含 标志时styleNumberStyles.AllowDecimalPoint小数位数才能显示在 中s
e e 或 E 字符,指示值以指数表示法。 如果style包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style

具有十进制数字的字符串仅 (对应于 NumberStyles.None 样式) 始终成功分析。 大多数剩余 NumberStyles 成员控制可能在此输入字符串中存在但不需要存在的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在 s的元素。

非复合 NumberStyles 值 除了数字外,还允许 在 中
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint fractional_digits 元素。 但是, fractional_digits 只能包含一个或多个 0 位数字,否则方法将 false返回 。
NumberStyles.AllowExponent 参数 s 还可以使用指数表示法。 如果 s 以指数表示法表示数字,则它必须表示数据类型范围内 Byte 没有非零的小数部分的整数。
NumberStyles.AllowLeadingWhite 开头的 sws 元素。
NumberStyles.AllowTrailingWhite 位于 末尾的 sws 元素。
NumberStyles.AllowLeadingSign 数字 前面可以显示正号。
NumberStyles.AllowTrailingSign 数字后面可能会出现正号。
NumberStyles.AllowParentheses 尽管支持此标志,但如果 中s存在括号,该方法将返回 false
NumberStyles.AllowThousands 尽管组分隔符可以出现在 中 s,但它前面只能有一个或多个 0 位数字。
NumberStyles.AllowCurrencySymbol $ 元素。

如果使用标志 NumberStyles.AllowHexSpecifiers 必须是不带前缀的十六进制值。 例如,“F3”分析成功,但“0xF3”则不成功。 中唯一可以存在的 style 其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 枚举具有复合数字样式 , NumberStyles.HexNumber其中包含两个空格标志。)

参数 provider 是实现 IFormatProvider ,如 CultureInfo 对象或 NumberFormatInfo 对象,其 GetFormat 方法返回 对象 NumberFormatInfo 。 对象 NumberFormatInfo 提供有关 格式的 s区域性特定信息。

另请参阅

适用于