Byte.TryParse 方法

定義

嘗試將數字的字串表示轉換成其相等的 Byte,並傳回一個值表示轉換是否成功。Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

多載

TryParse(String, Byte)

嘗試將數字的字串表示轉換成其相等的 Byte,並傳回一個值表示轉換是否成功。Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

TryParse(ReadOnlySpan<Char>, Byte)

嘗試將數字的範圍表示轉換為其對等的 Byte,並傳回值以指出轉換是否成功。Tries to convert the span representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

TryParse(String, NumberStyles, IFormatProvider, Byte)

將數字的字串表示 (使用指定樣式和特定文化特性的格式) 轉換為其相等的 ByteConverts the string representation of a number in a specified style and culture-specific format to its Byte equivalent. 傳回值,該值指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

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

將指定樣式和特定文化特性格式的數字範圍轉換為其對等 ByteConverts the span representation of a number in a specified style and culture-specific format to its Byte equivalent. 傳回值,該值指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

TryParse(String, Byte)

嘗試將數字的字串表示轉換成其相等的 Byte,並傳回一個值表示轉換是否成功。Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::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

字串,其包含要轉換的數字。A string that contains a number to convert.

result
Byte

當這個方法傳回時,如果轉換成功,則會包含相當於 Byte 中所含之數字的 s 值;如果轉換失敗則為零。When this method returns, contains the Byte value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

傳回

Boolean

如果 s 轉換成功,則為 true,否則為 falsetrue if s was converted successfully; otherwise, false.

範例

下列範例會 TryParse(String, Byte) 使用數個不同的字串值來呼叫方法。The following example calls the TryParse(String, Byte) method with a number of different string values.

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.
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.

備註

轉換失敗,而且 false 如果參數的格式不正確,則方法會傳回; s 如果是 nullString.Empty ,或如果表示小於 MinValue 或大於的數位 MaxValue ,則會傳回。The conversion fails and the method returns false if the s parameter is not in the correct format, if it is null or String.Empty, or if it represents a number less than MinValue or greater than MaxValue.

Byte.TryParse(String, Byte)方法類似于 Byte.Parse(String) 方法,但 TryParse(String, Byte) 如果轉換失敗,則不會擲回例外狀況。The Byte.TryParse(String, Byte) method is similar to the Byte.Parse(String) method, except that TryParse(String, Byte) does not throw an exception if the conversion fails.

s參數應該是數位的字串表示,格式如下:The s parameter should be the string representation of a number in the following form:

[ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

元素Element 描述Description
wsws 選擇性的空白字元。Optional white space.
簽署sign 由目前文化特性的屬性所指定的選擇性正號 NumberFormatInfo.PositiveSignAn optional positive sign, as specified by the NumberFormatInfo.PositiveSign property of the current culture.
數字digits 十進位數的序列,範圍從0到9。A sequence of decimal digits that range from 0 to 9.

s使用樣式來解讀參數 IntegerThe s parameter is interpreted using the Integer style. 除了位元組值的小數位數以外,只允許前置和尾端空格搭配前置正負號。In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (如果符號存在,則必須是正號,否則方法會擲回 OverflowException 。 ) 若要明確定義樣式元素,以及可以存在於中的特定文化特性格式資訊 s ,請使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。(If the sign is present, it must be a positive sign or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Byte.Parse(String, NumberStyles, IFormatProvider) method.

s參數會使用目前文化特性的物件中的格式資訊進行剖析 NumberFormatInfoThe s parameter is parsed using the formatting information in a NumberFormatInfo object for the current culture. 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfoFor more information, see NumberFormatInfo.CurrentInfo.

方法的這個多載會 Byte.TryParse(String, Byte) 將參數中的所有數位都解釋 s 為十進位數。This overload of the Byte.TryParse(String, Byte) method interprets all digits in the s parameter as decimal digits. 若要剖析十六進位數位的字串表示,請呼叫多載 Byte.TryParse(String, NumberStyles, IFormatProvider, Byte)To parse the string representation of a hexadecimal number, call the Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) overload.

另請參閱

適用於

TryParse(ReadOnlySpan<Char>, Byte)

嘗試將數字的範圍表示轉換為其對等的 Byte,並傳回值以指出轉換是否成功。Tries to convert the span representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded.

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>

範圍,其包含代表所要轉換數字的字元。A span containing the characters representing the number to convert.

result
Byte

當這個方法傳回時,如果轉換成功,則會包含相當於 Byte 中所含之數字的 s 值;如果轉換失敗則為零。When this method returns, contains the Byte value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

傳回

Boolean

如果 s 轉換成功,則為 true,否則為 falsetrue if s was converted successfully; otherwise, false.

適用於

TryParse(String, NumberStyles, IFormatProvider, Byte)

將數字的字串表示 (使用指定樣式和特定文化特性的格式) 轉換為其相等的 ByteConverts the string representation of a number in a specified style and culture-specific format to its Byte equivalent. 傳回值,該值指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::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

字串,包含要轉換的數字。A string containing a number to convert. 這個字串使用 style 指定的樣式來解譯。The string is interpreted using the style specified by style.

style
NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。An object that supplies culture-specific formatting information about s. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

result
Byte

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 8 位元不帶正負號整數,如果轉換失敗則為零。When this method returns, contains the 8-bit unsigned integer value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. 如果 s 參數為 nullEmpty、不是正確的格式,或是表示小於 MinValue 或大於 MaxValue 的數字,則轉換會失敗。The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

傳回

Boolean

如果 s 轉換成功,則為 true,否則為 falsetrue if s was converted successfully; otherwise, false.

例外狀況

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or-

style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

範例

下列範例會 TryParse(String, NumberStyles, IFormatProvider, Byte) 使用數個不同的字串值來呼叫方法。The following example calls the TryParse(String, NumberStyles, IFormatProvider, Byte) method with a number of different string values.

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.
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 如果轉換失敗,方法不會擲回例外狀況。The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.

s參數會使用參數所提供物件中的格式資訊進行剖析 NumberFormatInfo providerThe s parameter is parsed using the formatting information in a NumberFormatInfo object supplied by the provider parameter.

Style 參數會定義 (的樣式元素,例如空白字元或可在參數中允許的正負號) ,以便 s 讓剖析作業成功。The style parameter defines the style elements (such as white space or the positive sign) that are allowed in the s parameter for the parse operation to succeed. 它必須是來自列舉的位旗標組合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration. 視的值而定 styles 參數可能包含下列元素:Depending on the value of style, the s parameter may include the following elements:

atl-ws-01[$][sign] 位數 [.fractional_digits] [e [sign] 位數] [ws][ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

或者,如果 style 參數包含 AllowHexSpecifierOr, if the style parameter includes AllowHexSpecifier:

[ws] hexdigits [ws][ws]hexdigits[ws]

方括弧 ( [and] ) 中的元素是選擇性的。Elements in square brackets ( [ and ] ) are optional. 下表說明每個元素。The following table describes each element.

元素Element 描述Description
wsws 選擇性的空白字元。Optional white space. s如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或如果樣式包含旗標,則空格的開頭可能會出現空白字元 NumberStyles.AllowTrailingWhiteWhite space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 它在字串中的位置是由 NumberFormatInfo.CurrencyPositivePattern 參數方法所傳回之物件的屬性所定義 NumberFormatInfo GetFormat providerIts position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. s如果包含旗標,貨幣符號可以出現在中 style NumberStyles.AllowCurrencySymbolThe currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正號。An optional positive sign. (剖析作業會在中有負號時失敗 s 。 ) s 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則 s style NumberStyles.AllowTrailingSign 符號可以出現在開頭,如果包含旗標,則會出現在結尾。(The parse operation fails if a negative sign is present in s.) The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, or at the end of s if style includes the NumberStyles.AllowTrailingSign flag.
數字digits 從0到9的數位序列。A sequence of digits from 0 through 9.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果包含旗標,則所指定之文化特性的小數點符號 provider 可以出現在中 s style NumberStyles.AllowDecimalPointThe decimal point symbol of the culture specified by provider can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits 一或多個數位0出現。One or more occurrences of the digit 0. 只有在包含旗標時,小數位數才可以出現在中 s style NumberStyles.AllowDecimalPointFractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
pci-ee 表示值以指數標記法表示的 e 或 E 字元。The e or E character, which indicates that the value is represented in exponential notation. s如果包含旗標,則參數可以表示指數標記法中的數位 style NumberStyles.AllowExponentThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 從0到 f,或從0到 F 的十六進位數位序列。A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

s無論引數的值為何,剖析作業都會忽略中的任何終止 NUL (U + 0000) 字元 styleAny terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只有小數位數的字串, (對應到 NumberStyles.None 樣式) 一律會成功剖析。A string with decimal digits only (which corresponds to the NumberStyles.None style) always parses successfully. 大部分其餘的 NumberStyles 成員會控制可能存在但不需要出現在此輸入字串中的元素。Most of the remaining NumberStyles members control elements that may be but are not required to be present in this input string. 下表指出個別成員如何 NumberStyles 影響可能存在的元素 sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,還允許的元素Elements permitted in s in addition to digits
NumberStyles.None 僅限小數數位。Decimal digits only.
NumberStyles.AllowDecimalPoint .The . fractional_digits 元素。and fractional_digits elements. 不過, fractional_digits 必須只包含一或多個0位數,否則方法會傳回 falseHowever, fractional_digits must consist of only one or more 0 digits or the method returns false.
NumberStyles.AllowExponent s參數也可以使用指數標記法。The s parameter can also use exponential notation. 如果 s 以指數標記法表示數位,它必須代表資料類型範圍內的整數, Byte 而不含非零的小數部分。If s represents a number in exponential notation, it must represent an integer within the range of the Byte data type without a non-zero, fractional component.
NumberStyles.AllowLeadingWhite 開頭的 ws 元素 sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 結尾的 ws 元素 sThe ws element at the end of s.
NumberStyles.AllowLeadingSign 正負號可以出現在 數位 之前。A positive sign can appear before digits.
NumberStyles.AllowTrailingSign 數位 後面可以出現正負號。A positive sign can appear after digits.
NumberStyles.AllowParentheses 雖然支援此旗標,但如果有括弧,則方法 false 會傳回 sAlthough this flag is supported, the method returns false if parentheses are present in s.
NumberStyles.AllowThousands 雖然群組分隔符號符號可以出現在中 s ,但其前面只能有一個或多個0位數。Although the group separator symbol can appear in s, it can be preceded by only one or more 0 digits.
NumberStyles.AllowCurrencySymbol $ 元素。The $ element.

如果 NumberStyles.AllowHexSpecifier 使用旗標,則 s 必須是沒有前置詞的十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. 例如,"F3" 會成功剖析,但 "0xF3" 不會。For example, "F3" parses successfully, but "0xF3" does not. 中唯一可以存在的其他旗標 styleNumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be present in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (NumberStyles 列舉具有 NumberStyles.HexNumber 包含空白字元旗標的複合數位樣式。 ) (The NumberStyles enumeration has a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

provider參數是方法傳回 IFormatProvider 物件的實作為 CultureInfo 物件或 NumberFormatInfo 物件 GetFormat NumberFormatInfoThe provider parameter is an IFormatProvider implementation, such as a CultureInfo object or a NumberFormatInfo object, whose GetFormat method returns a NumberFormatInfo object. NumberFormatInfo物件提供有關格式的文化特性特定資訊 sThe NumberFormatInfo object provides culture-specific information about the format of s.

另請參閱

適用於

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

將指定樣式和特定文化特性格式的數字範圍轉換為其對等 ByteConverts the span representation of a number in a specified style and culture-specific format to its Byte equivalent. 傳回值,該值指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

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, 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>

範圍,其包含代表所要轉換數字的字元。A span containing the characters representing the number to convert. 範圍使用 Integer 樣式來解譯。The span is interpreted using the Integer style.

style
NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。An object that supplies culture-specific formatting information about s. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

result
Byte

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 8 位元不帶正負號整數,如果轉換失敗則為零。When this method returns, contains the 8-bit unsigned integer value equivalent to the number contained in s if the conversion succeeded, or zero if the conversion failed. 如果 s 參數為 nullEmpty、不是正確的格式,或是表示小於 MinValue 或大於 MaxValue 的數字,則轉換會失敗。The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。This parameter is passed uninitialized; any value originally supplied in result will be overwritten.

傳回

Boolean

如果 s 轉換成功,則為 true,否則為 falsetrue if s was converted successfully; otherwise, false.

適用於