共用方式為


UInt64.Parse 方法

定義

將數位的字串表示轉換為其相等的 64 位無符號整數。

多載

名稱 Description
Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其64位無符號整數對等專案。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式的數位範圍表示轉換成其 64 位無符號整數對等。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(String, IFormatProvider)

將指定文化特性特定格式之數位的字串表示轉換為其 64 位無符號整數對等。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(String)

將數位的字串表示轉換為其相等的 64 位無符號整數。

Parse(String, NumberStyles)

將指定樣式中數位的字串表示轉換為其相等的64位無符號整數。

Parse(String, NumberStyles, IFormatProvider)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Decimal.Parse(String)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其64位無符號整數對等專案。

public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::UInt64>::Parse;
[System.CLSCompliant(false)]
public static ulong Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static ulong Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ulong Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint64
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As ULong

參數

s
String

字串,表示要轉換的數位。 字串是透過參數指定的 style 樣式來解釋的。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

一個 64 位元無符號整數,等價於 中 s指定的數字。

實作

屬性

例外狀況

參數 snull

style 不是一個 NumberStyles 數值。

-或-

style不是 和 HexNumber 的組合AllowHexSpecifier

s 參數的格式不符合 style

s 參數代表小於 UInt64.MinValue 或大於 UInt64.MaxValue 的數字。

-或-

s 包含非零的分數數字。

範例

以下範例使用此 Parse(String, NumberStyles, IFormatProvider) 方法將各種字串表示數字轉換為 64 位元無符號整數值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] cultureNames= { "en-US", "fr-FR" };
      NumberStyles[] styles= { NumberStyles.Integer,
                               NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
      string[] values = { "170209", "+170209.0", "+170209,0", "-103214.00",
                                 "-103214,00", "104561.1", "104561,1" };
      
      // Parse strings using each culture
      foreach (string cultureName in cultureNames)
      {
         CultureInfo ci = new CultureInfo(cultureName);
         Console.WriteLine("Parsing strings using the {0} culture", 
                           ci.DisplayName);
         // Use each style.
         foreach (NumberStyles style in styles)
         {
            Console.WriteLine("   Style: {0}", style.ToString());
            // Parse each numeric string.
            foreach (string value in values)
            {
               try {
                  Console.WriteLine("      Converted '{0}' to {1}.", value,
                                    UInt64.Parse(value, style, ci));
               }
               catch (FormatException) {
                  Console.WriteLine("      Unable to parse '{0}'.", value);
               }      
               catch (OverflowException) {
                  Console.WriteLine("      '{0}' is out of range of the UInt64 type.",
                                    value);
               }
            }
         }
      }                                    
   }
}
// The example displays the following output:
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Converted '+170209.0' to 170209.
//          Unable to parse '+170209,0'.
//          '-103214.00' is out of range of the UInt64 type.
//          Unable to parse '-103214,00'.
//          '104561.1' is out of range of the UInt64 type.
//          Unable to parse '104561,1'.
//    Parsing strings using the French (France) culture
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Converted '+170209,0' to 170209.
//          Unable to parse '-103214.00'.
//          '-103214,00' is out of range of the UInt64 type.
//          Unable to parse '104561.1'.
//          '104561,1' is out of range of the UInt64 type.
open System
open System.Globalization

let cultureNames = [| "en-US"; "fr-FR" |]
let styles = [| NumberStyles.Integer; NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint |]
let values = 
    [| "170209"; "+170209.0"; "+170209,0"; "-103214.00"
       "-103214,00"; "104561.1"; "104561,1" |]

// Parse strings using each culture
for cultureName in cultureNames do
    let ci = CultureInfo cultureName
    printfn $"Parsing strings using the {ci.DisplayName} culture"
    // Use each style.
    for style in styles do
        printfn $"   Style: {style}"
        // Parse each numeric string.
        for value in values do
            try
                printfn $"      Converted '{value}' to {UInt64.Parse(value, style, ci)}."
            with
            | :? FormatException ->
                printfn $"      Unable to parse '{value}'."
            | :? OverflowException ->
                printfn $"      '{value}' is out of range of the UInt64 type."
// The example displays the following output:
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Converted '+170209.0' to 170209.
//          Unable to parse '+170209,0'.
//          '-103214.00' is out of range of the UInt64 type.
//          Unable to parse '-103214,00'.
//          '104561.1' is out of range of the UInt64 type.
//          Unable to parse '104561,1'.
//    Parsing strings using the French (France) culture
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Converted '+170209,0' to 170209.
//          Unable to parse '-103214.00'.
//          '-103214,00' is out of range of the UInt64 type.
//          Unable to parse '104561.1'.
//          '104561,1' is out of range of the UInt64 type.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR" }
      Dim styles() As NumberStyles = { NumberStyles.Integer, _
                                       NumberStyles.Integer Or NumberStyles.AllowDecimalPoint }
      Dim values() As String = { "170209", "+170209.0", "+170209,0", "-103214.00", _
                                 "-103214,00", "104561.1", "104561,1" }
      
      ' Parse strings using each culture
      For Each cultureName As String In cultureNames
         Dim ci As New CultureInfo(cultureName)
         Console.WriteLine("Parsing strings using the {0} culture", ci.DisplayName)
         ' Use each style.
         For Each style As NumberStyles In styles
            Console.WriteLine("   Style: {0}", style.ToString())
            ' Parse each numeric string.
            For Each value As String In values
               Try
                  Console.WriteLine("      Converted '{0}' to {1}.", value, _
                                    UInt64.Parse(value, style, ci))
               Catch e As FormatException
                  Console.WriteLine("      Unable to parse '{0}'.", value)   
               Catch e As OverflowException
                  Console.WriteLine("      '{0}' is out of range of the UInt64 type.", _
                                    value)         
               End Try
            Next
         Next
      Next                                    
   End Sub
End Module
' The example displays the following output:
'       Style: Integer
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Unable to parse '+170209,0'.
'          Unable to parse '-103214.00'.
'          Unable to parse '-103214,00'.
'          Unable to parse '104561.1'.
'          Unable to parse '104561,1'.
'       Style: Integer, AllowDecimalPoint
'          Converted '170209' to 170209.
'          Converted '+170209.0' to 170209.
'          Unable to parse '+170209,0'.
'          '-103214.00' is out of range of the UInt64 type.
'          Unable to parse '-103214,00'.
'          '104561.1' is out of range of the UInt64 type.
'          Unable to parse '104561,1'.
'    Parsing strings using the French (France) culture
'       Style: Integer
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Unable to parse '+170209,0'.
'          Unable to parse '-103214.00'.
'          Unable to parse '-103214,00'.
'          Unable to parse '104561.1'.
'          Unable to parse '104561,1'.
'       Style: Integer, AllowDecimalPoint
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Converted '+170209,0' to 170209.
'          Unable to parse '-103214.00'.
'          '-103214,00' is out of range of the UInt64 type.
'          Unable to parse '104561.1'.
'          '104561,1' is out of range of the UInt64 type.

備註

參數 style 定義了允許 s 在解析操作中成功使用風格元素(如空白或正負符號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。

根據 的 style值, s 參數可能包含以下元素:

[ws][$][手語]數字[。fractional_digits][E[sign]exponential_digits][ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 若 style 包含 NumberStyles.AllowHexSpecifier,該 s 參數可能包含以下元素:

[ws]六角數數字[ws]

下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果包含該旗幟,空白可以出現在 的s開頭,如果包含該NumberStyles.AllowTrailingWhite旗,則可以出現在 的結尾stylesNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由CurrencyPositivePattern參數方法provider回傳GetFormat的物件性質NumberFormatInfo所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。
簽署 選擇性符號。 (若s包含負號且表示非零數,則拋出 。OverflowException)標誌可以出現在 如果sstyle包含NumberStyles.AllowLeadingSign旗幟的開頭,也可以出現在 的結尾s,如果style包含了旗幟。NumberStyles.AllowTrailingSigns包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字 從 0 到 9 的數位序列。
. 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。
fractional_digits style 包含 NumberStyles.AllowExponent 該旗標,則表示出現一次或多次數字0;若不包含,則表示出現一次或多次數字0。 只有當 s 包含該旗幟時style,小數數字才能出現NumberStyles.AllowDecimalPoint在 中。
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
exponential_digits 從 0 到 9 的數位序列。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
六角位數 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有十進位數字的字串(對應 NumberStyles.None 於樣式)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非綜合 NumberStyles 除了數字外,允許加入 s 的元素
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 小數點(.)和 fractional_digits 元素。 但如果風格中沒有旗幟,NumberStyles.AllowExponentfractional_digits必須只包含一個或多個0數字;否則會丟出aOverflowException
NumberStyles.AllowExponent 「e」或「E」字元表示指數符號,並附有 exponential_digits
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
NumberStyles.AllowLeadingSign 數字 之前有符號。
NumberStyles.AllowTrailingSign 數字 後的符號。
NumberStyles.AllowParentheses 前後 數字 括號表示負值。
NumberStyles.AllowThousands 群分離子()元素。
NumberStyles.AllowCurrencySymbol 貨幣($)元素。

NumberStyles.AllowHexSpecifier 使用旗標, s 則必須為十六進位值。 有效的十六進位字元為0-9、A-F和 a-f。 不支援 「0x」 之類的前置詞,並導致剖析作業失敗。 唯一可以與之結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉包含一個複合數字樣式, NumberStyles.HexNumber包含兩個空白標記。)

注意

若參數 s 為十六進位數的字串表示,則其前不能加上任何裝飾(如 0x&h)來區分其為十六進位數。 這會導致剖析作業擲回例外狀況。

參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳 NumberFormatInfo 一個物件,提供關於 格式 s的文化特有資訊。 有三種方式可以利用該 provider 參數為解析操作提供自訂格式資訊:

provider為 ,nullNumberFormatInfo則使用 當前文化的物件。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

重要

此 API 不符合 CLS 規範。

將指定樣式和特定文化特性格式的數位範圍表示轉換成其 64 位無符號整數對等。

public static ulong Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static ulong Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static ulong Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint64
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As ULong

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。 該跨度是透過參數指定的 style 風格來解釋的。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

一個 64 位元無符號整數,等價於 中 s指定的數字。

實作

屬性

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

將UTF-8字元的範圍剖析為值。

public static ulong Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As ULong

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

style
NumberStyles

一個可以存在 utf8Text於 的數字樣式的位元組合。

provider
IFormatProvider

一個提供關於 utf8Text的文化特定格式資訊的物件。

傳回

解析 的結果 utf8Text

實作

適用於

Parse(String, IFormatProvider)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Decimal.Parse(String)

將指定文化特性特定格式之數位的字串表示轉換為其 64 位無符號整數對等。

public:
 static System::UInt64 Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::UInt64 Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::UInt64>::Parse;
[System.CLSCompliant(false)]
public static ulong Parse(string s, IFormatProvider provider);
public static ulong Parse(string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ulong Parse(string s, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint64
static member Parse : string * IFormatProvider -> uint64
Public Shared Function Parse (s As String, provider As IFormatProvider) As ULong

參數

s
String

字串,表示要轉換的數位。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

一個 64 位元無符號整數,等價於 中 s指定的數字。

實作

屬性

例外狀況

參數 snull

參數 s 的樣式不正確。

s 參數代表小於 UInt64.MinValue 或大於 UInt64.MaxValue 的數字。

範例

下列範例是 Web 窗體的按鈕點選事件處理程式。 它利用屬性 HttpRequest.UserLanguages 回傳的陣列來決定使用者的所在位置。 接著它實例化一個 CultureInfo 對應該地點的物件。 NumberFormatInfo屬於該CultureInfo物件的物件會被傳給Parse(String, IFormatProvider)將使用者輸入轉換成UInt64值的方法。

protected void OkToSingle_Click(object sender, EventArgs e)
{
    string locale;
    float number;
    CultureInfo culture;

    // Return if string is empty
    if (String.IsNullOrEmpty(this.inputNumber.Text))
        return;

    // Get locale of web request to determine possible format of number
    if (Request.UserLanguages.Length == 0)
        return;
    locale = Request.UserLanguages[0];
    if (String.IsNullOrEmpty(locale))
        return;

    // Instantiate CultureInfo object for the user's locale
    culture = new CultureInfo(locale);

    // Convert user input from a string to a number
    try
    {
        number = Single.Parse(this.inputNumber.Text, culture.NumberFormat);
    }
    catch (FormatException)
    {
        return;
    }
    catch (Exception)
    {
        return;
    }
    // Output number to label on web form
    this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
    Dim locale As String
    Dim culture As CultureInfo
    Dim number As Single

    ' Return if string is empty
    If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

    ' Get locale of web request to determine possible format of number
    If Request.UserLanguages.Length = 0 Then Exit Sub
    locale = Request.UserLanguages(0)
    If String.IsNullOrEmpty(locale) Then Exit Sub

    ' Instantiate CultureInfo object for the user's locale
    culture = New CultureInfo(locale)

    ' Convert user input from a string to a number
    Try
        number = Single.Parse(Me.inputNumber.Text, culture.NumberFormat)
    Catch ex As FormatException
        Exit Sub
    Catch ex As OverflowException
        Exit Sub
    End Try

    ' Output number to label on web form
    Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

備註

這種方法的 Parse(String, IFormatProvider) 過載通常用來將可以多種格式化的文字轉換為一個 UInt64 值。 例如,它可以用來將使用者輸入的文字轉換成 HTML 文字框,轉換為數值。

s 參數包含以下形式的數字:

[ws][sign]digits[ws]

方括弧 ([ 和 ]) 中的項目是選擇性專案。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。
簽署 可選的正號,或若代表值為零則 s 為負號。
數字 範圍從 0 到 9 的數位序列。

s 參數是透過 style NumberStyles.Integer 來解釋的。 除了無符號整數值的十進位數之外,只允許前置和尾端空格以及前置正負號。 (若有負號,則 s 必須代表零值,否則方法會拋出 。 OverflowException)若要明確定義風格元素及可存在 s的文化特定格式資訊,請使用該 Parse(String, NumberStyles, IFormatProvider) 方法。

參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳 NumberFormatInfo 一個物件,提供關於 格式 s的文化特有資訊。 有三種方式可以利用該 provider 參數為解析操作提供自訂格式資訊:

providernullNumberFormatInfo則 為當前文化。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

將字元範圍剖析為值。

public:
 static System::UInt64 Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::UInt64>::Parse;
public static ulong Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> uint64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As ULong

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

解析 的結果 s

實作

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

將UTF-8字元的範圍剖析為值。

public:
 static System::UInt64 Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::UInt64>::Parse;
public static ulong Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> uint64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As ULong

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

provider
IFormatProvider

一個提供關於 utf8Text的文化特定格式資訊的物件。

傳回

解析 的結果 utf8Text

實作

適用於

Parse(String)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Decimal.Parse(String)

將數位的字串表示轉換為其相等的 64 位無符號整數。

public:
 static System::UInt64 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static ulong Parse(string s);
public static ulong Parse(string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint64
static member Parse : string -> uint64
Public Shared Function Parse (s As String) As ULong

參數

s
String

字串,表示要轉換的數位。

傳回

一個 64 位元無符號整數,等價於 中 s所包含的數字。

屬性

例外狀況

參數 snull

參數 s 格式不正確。

s 參數代表小於 UInt64.MinValue 或大於 UInt64.MaxValue 的數字。

範例

以下範例使用此 Parse 方法解析一組字串值。

string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                    "0xFA1B", "163042", "-10", "14065839182",
                    "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
   try {
      ulong number = UInt64.Parse(value); 
      Console.WriteLine("{0} --> {1}", value, number);
   }
   catch (FormatException) {
      Console.WriteLine("{0}: Bad Format", value);
   }   
   catch (OverflowException) {
      Console.WriteLine("{0}: Overflow", value);   
   }  
}
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10: Overflow
//       14065839182 --> 14065839182
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow
let values = 
    [| "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
       "0xFA1B"; "163042"; "-10"; "14065839182"
       "16e07"; "134985.0"; "-12034" |]
for value in values do
    try 
        let number = UInt64.Parse value 
        printfn $"{value} --> {number}"
    with
    | :? FormatException ->
        printfn $"{value}: Bad Format"
    | :? OverflowException ->
        printfn $"{value}: Overflow"
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10: Overflow
//       14065839182 --> 14065839182
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow
Dim values() As String = { "+13230", "-0", "1,390,146", "$190,235,421,127", _
                           "0xFA1B", "163042", "-10", "14065839182", _
                           "16e07", "134985.0", "-12034" }
For Each value As String In values
   Try
      Dim number As ULong = UInt64.Parse(value) 
      Console.WriteLine("{0} --> {1}", value, number)
   Catch e As FormatException
      Console.WriteLine("{0}: Bad Format", value)
   Catch e As OverflowException
      Console.WriteLine("{0}: Overflow", value)   
   End Try  
Next
' The example displays the following output:
'       +13230 --> 13230
'       -0 --> 0
'       1,390,146: Bad Format
'       $190,235,421,127: Bad Format
'       0xFA1B: Bad Format
'       163042 --> 163042
'       -10: Overflow
'       14065839182 --> 14065839182
'       16e07: Bad Format
'       134985.0: Bad Format
'       -12034: Overflow

備註

s參數應為以下形式的數字的字串表示。

[ws][標誌]數字[WS]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。
簽署 選擇性符號。 有效的符號特徵是由 NumberFormatInfo.NegativeSign 當前文化的屬性 NumberFormatInfo.PositiveSign 決定的。 然而,負符號只能在零時使用;否則,該方法會拋出一個 OverflowException
數字 範圍從 0 到 9 的數位序列。 會忽略任何前置零。

注意

參數指定的 s 字串會透過 NumberStyles.Integer 樣式來解釋。 它不能包含任何群組分隔符或小數分隔符,而且不能有小數部分。

s參數是利用物件格式資訊解析,該物件已System.Globalization.NumberFormatInfo初始化為當前系統文化。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 要利用特定文化的格式資訊解析字串,請使用該 Parse(String, IFormatProvider) 方法。

另請參閱

適用於

Parse(String, NumberStyles)

來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs
來源:
UInt64.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Decimal.Parse(String)

將指定樣式中數位的字串表示轉換為其相等的64位無符號整數。

public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static ulong Parse(string s, System.Globalization.NumberStyles style);
public static ulong Parse(string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint64
static member Parse : string * System.Globalization.NumberStyles -> uint64
Public Shared Function Parse (s As String, style As NumberStyles) As ULong

參數

s
String

字串,表示要轉換的數位。 字串是透過參數指定的 style 樣式來解釋的。

style
NumberStyles

一個位元組合的列舉值,指定允許的 s格式。 典型的指定值為 Integer

傳回

一個 64 位元無符號整數,等價於 中 s指定的數字。

屬性

例外狀況

參數 snull

style 不是一個 NumberStyles 數值。

-或-

style不是 和 HexNumber 的組合AllowHexSpecifier

s 參數的格式不符合 style

s 參數代表小於 UInt64.MinValue 或大於 UInt64.MaxValue 的數字。

-或-

s 包含非零的分數數字。

範例

以下範例嘗試透過多個 NumberStyles 值解析字串陣列中的每個元素。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values= { " 214309 ", "1,064,181", "(0)", "10241+", " + 21499 ", 
                         " +21499 ", "122153.00", "1e03ff", "91300.0e-2" };
      NumberStyles whitespace =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
      NumberStyles[] styles= { NumberStyles.None, whitespace, 
                               NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace, 
                               NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol, 
                               NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };

      // Attempt to convert each number using each style combination.
      foreach (string value in values)
      {
         Console.WriteLine("Attempting to convert '{0}':", value);
         foreach (NumberStyles style in styles)
         {
            try {
               ulong number = UInt64.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }   
            catch (OverflowException)
            {
               Console.WriteLine("   {0}: Overflow", value);         
            }         
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    Attempting to convert ' 214309 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: 214309
//       Integer, AllowTrailingSign: 214309
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1,064,181':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: 1064181
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '(0)':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '10241+':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 10241
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' + 21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' +21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 21499
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '122153.00':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 122153
//    
//    Attempting to convert '1e03ff':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '91300.0e-2':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 913
open System
open System.Globalization

let values =
    [| " 214309 "; "1,064,181"; "(0)"; "10241+"; " + 21499 " 
       " +21499 "; "122153.00"; "1e03ff"; "91300.0e-2" |]
let whitespace =  NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
let styles =
    [| NumberStyles.None; whitespace 
       NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace 
       NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol 
       NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]

// Attempt to convert each number using each style combination.
for value in values do
    printfn $"Attempting to convert '{value}':"
    for style in styles do
        try
            let number = UInt64.Parse(value, style)
            printfn $"   {style}: {number}"
        with
        | :? FormatException ->
            printfn $"   {style}: Bad Format"
        | :? OverflowException ->
            printfn $"   {value}: Overflow"
    printfn ""
// The example displays the following output:
//    Attempting to convert ' 214309 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: 214309
//       Integer, AllowTrailingSign: 214309
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1,064,181':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: 1064181
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '(0)':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '10241+':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 10241
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' + 21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' +21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 21499
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '122153.00':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 122153
//    
//    Attempting to convert '1e03ff':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '91300.0e-2':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 913
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { " 214309 ", "1,064,181", "(0)", "10241+", _
                                 " + 21499 ", " +21499 ", "122153.00", _
                                 "1e03ff", "91300.0e-2" }
      Dim whitespace As NumberStyles =  NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite
      Dim styles() As NumberStyles = { NumberStyles.None, _
                                       whitespace, _
                                       NumberStyles.AllowLeadingSign Or NumberStyles.AllowTrailingSign Or whitespace, _
                                       NumberStyles.AllowThousands Or NumberStyles.AllowCurrencySymbol, _
                                       NumberStyles.AllowExponent Or NumberStyles.AllowDecimalPoint }

      ' Attempt to convert each number using each style combination.
      For Each value As String In values
         Console.WriteLine("Attempting to convert '{0}':", value)
         For Each style As NumberStyles In styles
            Try
               Dim number As ULong = UInt64.Parse(value, style)
               Console.WriteLine("   {0}: {1}", style, number)
            Catch e As FormatException
               Console.WriteLine("   {0}: Bad Format", style)
            Catch e As OverflowException
               Console.WriteLine("   {0}: Overflow", value)         
            End Try         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'    Attempting to convert ' 214309 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: 214309
'       Integer, AllowTrailingSign: 214309
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '1,064,181':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: 1064181
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '(0)':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '10241+':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: 10241
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert ' + 21499 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert ' +21499 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: 21499
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '122153.00':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: 122153
'    
'    Attempting to convert '1e03ff':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '91300.0e-2':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: 913

備註

參數 style 定義了允許在解析操作成功時所允許 s 的樣式元素(例如空白、正負符號、群組分隔符號或小數點符號)。 style 必須是列舉中多個位元旗 NumberStyles 標的組合。 styles 包含十六進位值的字串表示、所表示s的數字系統(十進位或十六進位)僅在執行時已知,或你想在 中排除空白或符號s符號時,這個參數使此方法過載變得有用。

根據 的 style值, s 參數可能包含以下元素:

[ws][$][符號][數字,]數字[。fractional_digits][E[sign]exponential_digits][ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 若 style 包含 NumberStyles.AllowHexSpecifier,該 s 參數可能包含以下元素:

[ws]六角數數字[ws]

下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果包含旗幟,空白可以出現在 的s開頭,如果style包含 旗NumberStyles.AllowLeadingWhite幟,則可以出現在結尾sstyleNumberStyles.AllowTrailingWhite
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
簽署 選擇性符號。 如果包含旗幟,標誌可以出現在 的s開頭,如果包含style該旗幟,則可以出現在 的結尾NumberStyles.AllowLeadingSignsstyleNumberStyles.AllowTrailingSigns包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。 然而,負符號只能在零時使用;否則,該方法會拋出一個 OverflowException
數字

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 對於 fractional_digits,只有數字0有效。
, 特定文化特性的群組分隔符符號。 當前文化的群組分隔符可以出現s在包含styleNumberStyles.AllowThousands旗幟時。
. 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。 只有數字 0 可以以小數數字出現,才能成功進行解析操作;若 fractional_digits 包含其他數字,則擲出 A FormatException
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
六角位數 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有數字的字串(對應 NumberStyles.None 於樣式)若在該類型範圍內 UInt64 ,則能成功解析。 大多數剩餘 NumberStyles 成員控制輸入字串中可能存在但不必須存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

NumberStyles 除了數字外,允許加入 s 的元素
None 只有 數字 元素。
AllowDecimalPoint 小數點(.)和 小數數字 元素。
AllowExponent 「e」或「E」字元表示指數符號,並附有 exponential_digits
AllowLeadingWhite 該元素位於 的開頭s 元素。
AllowTrailingWhite 末尾s 元素。
AllowLeadingSign 符號 元素位於 的開頭 s
AllowTrailingSign 符號 元素位於 的結尾 s
AllowParentheses 符號 元素以 括號形式包含數值。
AllowThousands 群組分隔符 (,) 專案。
AllowCurrencySymbol currency ($) 元素。
Currency 所有元素。 然而,無法 s 表示十六進位數或指數符號中的數字。
Float 起始或結尾s元素,符號開頭s,以及小數點(.)符號。 參數 s 也可以使用指數符號。
Number wssign、 群組分隔符 (,) 以及小數點 (.) 元素。
Any 所有元素。 然而,無法 s 代表十六進位數。

與其他NumberStyles允許但不要求存在特定樣式元素sNumberStyles.AllowHexSpecifier的值不同,樣式值意味著 中的s個別數字字元總是被解釋為十六進位字元。 有效的十六進位字元為0-9、A-F和 a-f。 不支援 「0x」 之類的前置詞,並導致剖析作業失敗。 唯一可以與參數 style 結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉包含一個複合數字樣式, NumberStyles.HexNumber包含兩個空白標記。)

注意

s 是十六進位數的字串表示,則其前不能加上任何裝飾(如 0x&h)來區分其為十六進位數。 這會導致轉換失敗。

s參數是利用物件格式資訊解析,該物件已NumberFormatInfo初始化為當前系統文化。 若要指定用於解析操作的格式資訊文化,請呼叫 overload Parse(String, NumberStyles, IFormatProvider)

另請參閱

適用於