UInt16.TryParse UInt16.TryParse UInt16.TryParse UInt16.TryParse Method

定義

將數字的字串表示轉換成它的對等 16 位元不帶正負號的整數。Converts the string representation of a number to its 16-bit unsigned integer equivalent. 傳回值會指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

多載

TryParse(String, UInt16) TryParse(String, UInt16) TryParse(String, UInt16) TryParse(String, UInt16)

嘗試將數字的字串表示轉換為 16 位元不帶正負號的整數。Tries to convert the string representation of a number to its 16-bit unsigned integer equivalent. 傳回值會指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, UInt16) TryParse(ReadOnlySpan<Char>, UInt16) TryParse(ReadOnlySpan<Char>, UInt16) TryParse(ReadOnlySpan<Char>, UInt16)
TryParse(String, NumberStyles, IFormatProvider, UInt16) TryParse(String, NumberStyles, IFormatProvider, UInt16) TryParse(String, NumberStyles, IFormatProvider, UInt16) TryParse(String, NumberStyles, IFormatProvider, UInt16)

嘗試將指定之樣式及特定文化特性格式資訊數字的字串表示,轉換為它的 16 位元不帶正負號的整數對應項。Tries to convert the string representation of a number in a specified style and culture-specific format to its 16-bit unsigned integer equivalent. 傳回值會指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

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

TryParse(String, UInt16) TryParse(String, UInt16) TryParse(String, UInt16) TryParse(String, UInt16)

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int32.TryParse(String, Int32)

嘗試將數字的字串表示轉換為 16 位元不帶正負號的整數。Tries to convert the string representation of a number to its 16-bit unsigned integer equivalent. 傳回值會指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::UInt16 % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out ushort result);
static member TryParse : string * uint16 -> bool
Public Shared Function TryParse (s As String, ByRef result As UShort) As Boolean

參數

s
String String String String

字串,表示要轉換的數字。A string that represents the number to convert.

result
UInt16 UInt16 UInt16 UInt16

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 16 位元不帶正負號整數,如果轉換失敗則為零。When this method returns, contains the 16-bit unsigned integer value that is equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. 如果 s 參數為 nullEmpty,或者不是正確的格式,則轉換會失敗。The conversion fails if the s parameter is null or Empty, is not in the correct format. ,或代表小於 MinValue 或大於 MaxValue 的數字。, 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.

傳回

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

範例

下列範例會針對字串TryParse(String, UInt16)陣列中的每個元素呼叫一次方法。The following example calls the TryParse(String, UInt16) method once for each element in a string array.

string[] numericStrings = { "1293.8", "+1671.7", "28347.",  
                            "   33113684  ", "(0)", "-0", "-1", 
                            "+1293617", "18-", "119870", "31,024", 
                            "  3127094 ", "00700000" };
uint number;
foreach (string numericString in numericStrings)
{
   if (UInt32.TryParse(numericString, out number)) 
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
   else
      Console.WriteLine("Cannot convert '{0}' to a UInt32.", numericString);
}
// The example displays the following output:
//       Cannot convert '1293.8' to a UInt32.
//       Cannot convert '+1671.7' to a UInt32.
//       Cannot convert '28347.' to a UInt32.
//       Converted '   33113684  ' to 33113684.
//       Cannot convert '(0)' to a UInt32.
//       Converted '-0' to 0.
//       Cannot convert '-1' to a UInt32.
//       Converted '+1293617' to 1293617.
//       Cannot convert '18-' to a UInt32.
//       Converted '119870' to 119870.
//       Cannot convert '31,024' to a UInt32.
//       Converted '  3127094 ' to 3127094.
//       Converted '0070000' to 70000.
Dim numericStrings() As String = {"1293.8", "+1671.7", "28347.", 
                                  "   33113684  ", "(0)", "-0", "-1",
                                  "+1293617", "18-", "119870", 
                                  "31,024", "  3127094 ", "0070000" }
Dim number As UInteger
For Each numericString As String In numericStrings
   If UInt32.TryParse(numericString, number) Then
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
   Else
      Console.WriteLine("Cannot convert '{0}' to a UInt32.", numericString)
   End If      
Next
' The example displays the following output:
'       Cannot convert '1293.8' to a UInt32.
'       Cannot convert '+1671.7' to a UInt32.
'       Cannot convert '28347.' to a UInt32.
'       Converted '   33113684  ' to 33113684.
'       Cannot convert '(0)' to a UInt32.
'       Converted '-0' to 0.
'       Cannot convert '-1' to a UInt32.
'       Converted '+1293617' to 1293617.
'       Cannot convert '18-' to a UInt32.
'       Converted '119870' to 119870.
'       Cannot convert '31,024' to a UInt32.
'       Converted '  3127094 ' to 3127094.
'       Converted '0070000' to 70000.

備註

方法與Parse(String)方法類似, 不同之處在于它不會在轉換失敗時擲回例外狀況。 TryParse(String, UInt16)The TryParse(String, UInt16) method is like the Parse(String) method, except that it does not throw an exception if the conversion fails. 這個方法不需要使用例外狀況處理來測試FormatException if s是否無效, 而且無法成功剖析。This method eliminates the need to use exception handling to test for a FormatException if s is invalid and cannot be successfully parsed.

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

[ws][sign]digits[ws][ws][sign]digits[ws]

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

元素Element 說明Description
wsws 選擇性的空白字元。Optional white space.
簽署sign 選擇性的正負號。An optional sign. 有效的正負號字元是由NumberFormatInfo.NegativeSign目前NumberFormatInfo.PositiveSign文化特性的和屬性所決定。Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture.
digitsdigits 十進位數的序列, 範圍介於0到9之間。A sequence of decimal digits ranging from 0 to 9.

注意

s參數所指定的字串不能包含任何群組分隔符號或小數分隔符號, 而且不能有小數部分。The string specified by the s parameter cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

使用樣式來解讀s參數 NumberStyles.IntegerThe s parameter is interpreted by using the NumberStyles.Integer style. 除了十進位數以外, 只允許開頭和尾端空格加上前置正負號。In addition to the decimal digits, only leading and trailing spaces with a leading sign are allowed. 若要使用中s可存在的特定文化特性格式資訊, 明確地定義樣式元素, 請TryParse(String, NumberStyles, IFormatProvider, UInt16)呼叫方法。To explicitly define the style elements with the culture-specific formatting information that can be present in s, call the TryParse(String, NumberStyles, IFormatProvider, UInt16) method.

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

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

另請參閱

TryParse(ReadOnlySpan<Char>, UInt16) TryParse(ReadOnlySpan<Char>, UInt16) TryParse(ReadOnlySpan<Char>, UInt16) TryParse(ReadOnlySpan<Char>, UInt16)

重要

此 API 不符合 CLS 規範。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::UInt16 % result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out ushort result);
static member TryParse : ReadOnlySpan<char> * uint16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As UShort) As Boolean

參數

傳回

TryParse(String, NumberStyles, IFormatProvider, UInt16) TryParse(String, NumberStyles, IFormatProvider, UInt16) TryParse(String, NumberStyles, IFormatProvider, UInt16) TryParse(String, NumberStyles, IFormatProvider, UInt16)

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int32.TryParse(String, Int32)

嘗試將指定之樣式及特定文化特性格式資訊數字的字串表示,轉換為它的 16 位元不帶正負號的整數對應項。Tries to convert the string representation of a number in a specified style and culture-specific format to its 16-bit unsigned integer 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::UInt16 % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out ushort result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As UShort) As Boolean

參數

s
String String String String

字串,表示要轉換的數字。A string that represents the number to convert. 這個字串使用 style 參數指定的樣式來解譯。The string is interpreted by using the style specified by the style parameter.

style
NumberStyles NumberStyles NumberStyles NumberStyles

列舉值的位元組合,表示 s 所允許的格式。A bitwise combination of enumeration values that indicates the permitted format of s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

物件,提供關於 s 的特定文化特性格式資訊。An object that supplies culture-specific formatting information about s.

result
UInt16 UInt16 UInt16 UInt16

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 16 位元不帶正負號整數,如果轉換失敗則為零。When this method returns, contains the 16-bit unsigned integer value equivalent to the number contained in s, if the conversion succeeded, or zero if the conversion failed. 轉換失敗的狀況包括:如果 s 參數為 nullEmpty、格式與 style 不相容,或是表示小於 MinValue 或大於 MaxValue 的數字。The conversion fails if the s parameter is null or Empty, is not in a format compliant with style, 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.

傳回

如果 true 轉換成功,則為 s,否則為 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, UInt16)個不同的字串和NumberStyles值來呼叫方法。The following example calls the TryParse(String, NumberStyles, IFormatProvider, UInt16) method with a number of different strings and NumberStyles values.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;
      
      numericString = "10603";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);
      
      numericString = "-10603";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);
      
      numericString = "29103.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);
      
      numericString = "10345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "2210E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 
      
      numericString = "9112E-01";
      CallTryParse(numericString, styles);
          
      numericString = "312E01";
      CallTryParse(numericString, styles); 
      
      numericString = "FFC8";
      CallTryParse(numericString, NumberStyles.HexNumber);
      
      numericString = "0x8F8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }
   
   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      ushort number;
      bool result = UInt16.TryParse(stringToConvert, styles, 
                                   CultureInfo.InvariantCulture, out number);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.", 
                           Convert.ToString(stringToConvert));
   }
}
// The example displays the following output:
//       Converted '10603' to 10603.
//       Attempted conversion of '-10603' failed.
//       Converted '29103.00' to 29103.
//       Attempted conversion of '10345.72' failed.
//       Converted '2210E-01' to 221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC8' to 65480.
//       Attempted conversion of '0x8F8C' failed.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "10603"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-10603"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      numericString = "29103.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "10345.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "2210E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
      
      numericString = "9112E-01"
      CallTryParse(numericString, styles)
          
      numericString = "312E01"
      CallTryParse(numericString, styles) 
      
      numericString = "FFC8"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x8F8C"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As UShort
      Dim result As Boolean = UInt16.TryParse(stringToConvert, styles, _
                                              CultureInfo.InvariantCulture, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           Convert.ToString(stringToConvert))
      End If                                                                           
   End Sub
End Module
' The example displays the following output to the console:
'       Converted '10603' to 10603.
'       Attempted conversion of '-10603' failed.
'       Converted '29103.00' to 29103.
'       Attempted conversion of '10345.72' failed.
'       Converted '2210E-01' to 221.
'       Attempted conversion of '9112E-01' failed.
'       Converted '312E01' to 3120.
'       Converted 'FFC8' to 65480.
'       Attempted conversion of '0x8F8C' failed.

備註

方法與Parse(String, NumberStyles, IFormatProvider)方法類似, 不同之處在于它不會在轉換失敗時擲回例外狀況。 TryParse(String, NumberStyles, IFormatProvider, UInt16)The TryParse(String, NumberStyles, IFormatProvider, UInt16) method is like the Parse(String, NumberStyles, IFormatProvider) method, except that it does not throw an exception if the conversion fails. 這個方法不需要使用例外狀況處理來測試FormatException if s是否無效, 而且無法成功剖析。This method eliminates the need to use exception handling to test for a FormatException if s is invalid and cannot be parsed successfully.

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

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws][ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

以方括弧([和])括住的專案是選擇性的。Items in square brackets ([ and ]) are optional. 或者,如果style參數包含AllowHexSpecifiers參數可能會包含下列元素:Or, if the style parameter includes AllowHexSpecifier, the s parameter may include the following elements:

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

下表說明每個元素。The following table describes each element.

wsws
選擇性的空白字元。Optional white space. s s如果包含style 旗標NumberStyles.AllowTrailingWhite , 或在包含旗標的結尾處,空白字元可能會出現在開頭。style NumberStyles.AllowLeadingWhiteWhite space can appear at the start 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. 其在字串中的位置CurrencyPositivePattern是由provider參數的GetFormat方法所NumberFormatInfo傳回之物件的屬性所定義。Its position in the string is defined by the 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 sign. s s style NumberStyles.AllowTrailingSign如果包含style 旗標,則正負號可能會出現在開頭,而且如果包含旗標,它可以NumberStyles.AllowLeadingSign出現在的結尾。The sign can appear at the start of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. 如果包含s style 旗標,則可以在中使用括弧來NumberStyles.AllowParentheses表示負數值。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag. 不過,如果有負號,則只能s代表零值,讓剖析作業成功。However, if the negative sign is present, s can only represent the value zero for the parse operation to succeed.

digitsdigits
從0到9的數位序列。A sequence of digits from 0 through 9.

,
文化特性特定的群組分隔符號。A culture-specific group separator. provider s如果包含NumberStyles.AllowThousands旗標, 則所指定文化特性的群組分隔符號可以出現在中。 styleThe group separator of the culture specified by provider can appear in s if style includes the NumberStyles.AllowThousands flag.

..
文化特性特定的小數點符號。A culture-specific decimal point symbol. provider s如果包含NumberStyles.AllowDecimalPoint旗標, 則所指定文化特性的小數點符號會出現在中。 styleThe 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.

EE
"E" 或 "E" 字元, 表示該值是以指數 (科學) 標記法表示。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. 如果s 包含旗NumberStyles.AllowExponent標, 參數可以表示指數標記法中的數位。 styleThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

exponential_digitsexponential_digits
從0到9的數位序列。A sequence of digits from 0 through 9. 如果s 包含旗NumberStyles.AllowExponent標, 參數可以表示指數標記法中的數位。 styleThe 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.

注意

不論style引數的值為何, 剖析作業都會s忽略中的任何終止 NUL (U + 0000) 字元。Any 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 flag) always parses successfully. 在此輸入字串NumberStyles中, 大多數可能存在但不需要存在的其餘成員控制項元素。Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in this input string. 下表指出個別NumberStyles成員如何影響中s可能存在的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合NumberStylesNon-composite NumberStyles values 除了數位之外, 值中允許的元素Elements permitted in value in addition to digits
None 僅限十進位數。Decimal digits only.
AllowDecimalPoint 小數點(.)和fractional_digits元素。The decimal point (.) and fractional_digits elements. 不過, fractional_digits必須僅包含一個或多個0位數,否則方法false會傳回。However, fractional_digits must consist of only one or more 0 digits, or the method returns false.
AllowExponent "E" 或 "E" 字元,表示指數標記法,以及exponential_digitsThe "e" or "E" character, which indicates exponential notation, along with exponential_digits. 如果s代表指數標記法中的數位, 則不能有非零的小數部分。If s represents a number in exponential notation, it cannot have a non-zero, fractional component.
AllowLeadingWhite 開頭sws元素。The ws element at the start of s.
AllowTrailingWhite 結尾sws元素。The ws element at the end of s.
AllowLeadingSign 數位前面的正負號元素。The sign element before digits.
AllowTrailingSign 數位後面的sign元素。The sign element after digits.
AllowParentheses 以括弧形式括住零數值的sign元素。The sign element in the form of parentheses enclosing a zero numeric value.
AllowThousands 群組分隔符號 (,) 元素。The group separator (,) element.
AllowCurrencySymbol Currency ($) 元素。The currency ($) element.
Currency 所有元素。All elements. 不過, s不能以指數標記法表示十六進位數位或數位。However, s cannot represent a hexadecimal number or a number in exponential notation.
Float 在開頭或結尾s的 ws 元素, 在的開頭s和小數點 (.) 符號之間進行正負號The ws element at the start or end of s, sign at the start of s, and the decimal point (.) symbol. s參數也可以使用指數標記法。The s parameter can also use exponential notation.
Number Wssign、group 分隔符號 () 和小數點 (.) 元素。The ws, sign, group separator (,), and decimal point (.) elements.
Any 所有元素。All elements. 不過, s不能代表十六進位數位。However, s cannot represent a hexadecimal number.

s如果使用NumberStyles.AllowHexSpecifier旗標, 必須是十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value. 有效的十六進位數位為0到9、a 到 f,以及 A 到 F。不支援前置詞(例如 "0x"),且會導致剖析作業失敗。Valid hexadecimal digits are 0 through 9, a through f, and A through F. A prefix such as "0x" is not supported and causes the parse operation to fail. style唯一可以出現的其他旗標是NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be present in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (列舉具有複合樣式, HexNumber其中同時包含空白字元旗標)。 NumberStyles(The NumberStyles enumeration has a composite style, HexNumber, that includes both white-space flags.)

注意

如果s是十六進位數位的字串表示, 其前面不能加上與十六進位數位區別的任何0x裝飾&h(例如或)。If s is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. 這會導致轉換失敗。This causes the conversion to fail.

參數是一種IFormatProvider實作為。 providerThe provider parameter is an IFormatProvider implementation. GetFormat方法s會傳回物件, 以提供有關格式的特定文化特性資訊。 NumberFormatInfoIts GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. provider參數可以是下列任何一項:The provider parameter can be any one of the following:

如果providernull ,NumberFormatInfo則會使用目前文化特性的物件。If provider is null, the NumberFormatInfo object for the current culture is used.

另請參閱

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

重要

此 API 不符合 CLS 規範。

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out ushort result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As UShort) As Boolean

參數

傳回

適用於