共用方式為


UInt32.Parse 方法

定義

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

多載

Parse(String, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(String)

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

Parse(String, NumberStyles)

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

Parse(String, NumberStyles, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

重要

此 API 不符合 CLS 規範。

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

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

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

參數

s
String

字串,表示要轉換的數位。 字串是使用 style 參數所指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Integer

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

s中指定的數位相等的32位無符號整數。

實作

屬性

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style不相容。

s 代表小於 UInt32.MinValue 或大於 UInt32.MaxValue的數位。

-或-

s 包含非零的小數位數。

範例

下列範例使用 Parse(String, NumberStyles, IFormatProvider) 方法,將數位的各種字串表示轉換成 32 位無符號整數值。

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,
                                    UInt32.Parse(value, style, ci));
               }
               catch (FormatException) {
                  Console.WriteLine("      Unable to parse '{0}'.", value);
               }      
               catch (OverflowException) {
                  Console.WriteLine("      '{0}' is out of range of the UInt32 type.",
                                    value);
               }
            }
         }
      }                                    
   }
}
// The example displays the following output:
//       Parsing strings using the English (United States) 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.
//             Converted '+170209.0' to 170209.
//             Unable to parse '+170209,0'.
//             '-103214.00' is out of range of the UInt32 type.
//             Unable to parse '-103214,00'.
//             '104561.1' is out of range of the UInt32 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 UInt32 type.
//             Unable to parse '104561.1'.
//             '104561,1' is out of range of the UInt32 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 {UInt32.Parse(value, style, ci)}."
            with
            | :? FormatException ->
                printfn $"      Unable to parse '{value}'."
            | :? OverflowException ->
                printfn $"      '{value}' is out of range of the UInt32 type."
// The example displays the following output:
//       Parsing strings using the English (United States) 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.
//             Converted '+170209.0' to 170209.
//             Unable to parse '+170209,0'.
//             '-103214.00' is out of range of the UInt32 type.
//             Unable to parse '-103214,00'.
//             '104561.1' is out of range of the UInt32 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 UInt32 type.
//             Unable to parse '104561.1'.
//             '104561,1' is out of range of the UInt32 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, _
                                    UInt32.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 UInt32 type.", _
                                    value)         
               End Try
            Next
         Next
      Next                                    
   End Sub
End Module
' The example displays the following output:
'       Parsing strings using the English (United States) 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.
'             Converted '+170209.0' to 170209.
'             Unable to parse '+170209,0'.
'             '-103214.00' is out of range of the UInt32 type.
'             Unable to parse '-103214,00'.
'             '104561.1' is out of range of the UInt32 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 UInt32 type.
'             Unable to parse '104561.1'.
'             '104561,1' is out of range of the UInt32 type.

備註

style 參數會定義 s 參數中允許的樣式專案(例如空格符或正負號符號),讓剖析作業成功。 它必須是來自 NumberStyles 列舉的位旗標組合。

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

[ws][$][符號]位數[.fractional_digits][E[符號]exponential_digits][ws] ]

方括弧 ([ 和 ]) 中的元素是選擇性的。 如果 style 包含 NumberStyles.AllowHexSpecifiers 參數可能包含下列元素:

[ws]hexdigits[ws]

下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則會在 s 開頭顯示空格符,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由 provider 參數之 GetFormat 方法所傳回之 NumberFormatInfo 物件的 CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,貨幣符號可以出現在 s 中。
簽署 選擇性符號。 (如果 s 包含負號,且表示非零的數位,則方法會擲回 OverflowException。如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號可能會出現在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。
位數 從 0 到 9 的數位序列。
特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。
fractional_digits 如果 style 包含 NumberStyles.AllowExponent 旗標,則為 0-9 的一或多個數位,如果沒有,則為一或多個數位 0 的出現次數。 只有 style 包含 NumberStyles.AllowDecimalPoint 旗標時,小數位數才會出現在 s 中。
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
exponential_digits 從 0 到 9 的數位序列。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論 style 自變數的值為何,剖析作業都會忽略 s 中任何終止的 NUL (U+0000) 字元。

只有小數字數的字串(對應至 NumberStyles.None 樣式)一律會成功剖析。 其餘大部分 NumberStyles 成員控件元素可能存在,但不需要存在,在此輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

非複合 NumberStyles 除了數位以外,s 中允許的專案
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 小數點 (.) 和 fractional_digits 專案。 不過,如果樣式不包含 NumberStyles.AllowExponent 旗標,fractional_digits 必須只包含一或多個0位數;否則,會擲回 OverflowException
NumberStyles.AllowExponent “e” 或 “E” 字元,表示指數表示法,以及 exponential_digits
NumberStyles.AllowLeadingWhite s開頭的 ws ws 專案。
NumberStyles.AllowTrailingWhite s結尾的 ws 專案。
NumberStyles.AllowLeadingSign 數位之前的符號。
NumberStyles.AllowTrailingSign 數字之後的符號。
NumberStyles.AllowParentheses 括弧前後 數位 表示負值。
NumberStyles.AllowThousands 群組分隔符 () 元素。
NumberStyles.AllowCurrencySymbol currency ($) 元素。

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是十六進位值。 唯一可以與它結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉包含包含兩個空格符旗標的複合數字樣式 NumberStyles.HexNumber

注意

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

provider 參數是 IFormatProvider 實作,其 GetFormat 方法會傳回 NumberFormatInfo 物件,以提供 s格式的文化特性特定資訊。 有三種方式可以使用 provider 參數,將自定義格式資訊提供給剖析作業:

如果 providernull,則會使用目前文化特性的 NumberFormatInfo 物件。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

重要

此 API 不符合 CLS 規範。

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

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

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。 範圍是使用 style 參數所指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Integer

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

s中指定的數位相等的32位無符號整數。

實作

屬性

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

Parse(String, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

重要

此 API 不符合 CLS 規範。

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

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

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

參數

s
String

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

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

s中指定的數位相等的32位無符號整數。

實作

屬性

例外狀況

s 不是正確的樣式。

s 代表小於 UInt32.MinValue 或大於 UInt32.MaxValue的數位。

範例

下列範例是 Web 窗體的按鈕點選事件處理程式。 它會使用 HttpRequest.UserLanguages 屬性所傳回的數位來判斷使用者的地區設定。 然後,它會具現化對應至該地區設定的 CultureInfo 物件。 屬於該 CultureInfo 物件的 NumberFormatInfo 對象接著會傳遞至 Parse(String, IFormatProvider) 方法,將使用者的輸入轉換成 UInt32 值。

protected void OkToUInteger_Click(object sender, EventArgs e)
{
    string locale;
    uint 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 = UInt32.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 OKToUInteger_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OKToUInteger.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As UInteger

   ' 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 = UInt32.Parse(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As Exception
      Exit Sub
   End Try

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

備註

s 參數包含一些表單:

[ws][符號]數位[ws]

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

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

s 參數會使用 NumberStyles.Integer 樣式來解譯。 除了無符號整數值的十進位數之外,只允許前置和尾端空格以及前置正負號。 (如果負號存在,s 必須代表零的值,否則方法會擲回 OverflowException。若要明確定義樣式專案以及可存在於 s中的特定文化特性格式資訊,請使用 Parse(String, NumberStyles, IFormatProvider) 方法。

provider 參數是 IFormatProvider 實作,其 GetFormat 方法會傳回 NumberFormatInfo 物件,以提供 s格式的文化特性特定資訊。 有三種方式可以使用 provider 參數,將自定義格式資訊提供給剖析作業:

如果 providernull,則會使用目前文化特性的 NumberFormatInfo

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

將字元範圍剖析為值。

public:
 static System::UInt32 Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::UInt32>::Parse;
public static uint Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> uint32
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As UInteger

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

物件,提供與 s相關的特定文化特性格式資訊。

傳回

剖析 s的結果。

實作

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
UInt32.cs
來源:
UInt32.cs

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

public:
 static System::UInt32 Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::UInt32>::Parse;
public static uint Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> uint32
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As UInteger

參數

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

傳回

剖析 utf8Text的結果。

實作

適用於

Parse(String)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

重要

此 API 不符合 CLS 規範。

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

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

public:
 static System::UInt32 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static uint Parse (string s);
public static uint Parse (string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint32
static member Parse : string -> uint32
Public Shared Function Parse (s As String) As UInteger

參數

s
String

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

傳回

32 位無符號整數,相當於 s中包含的數位。

屬性

例外狀況

s 參數是 null

s 參數的格式不正確。

s 參數代表小於 UInt32.MinValue 或大於 UInt32.MaxValue的數位。

範例

下列範例會使用 Parse(String) 方法來剖析字串值的陣列。

string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                    "0xFA1B", "163042", "-10", "2147483648", 
                    "14065839182", "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
   try {
      uint number = UInt32.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
//       2147483648 --> 2147483648
//       14065839182: Overflow
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow
open System

let values = 
    [| "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
       "0xFA1B"; "163042"; "-10"; "2147483648"
       "14065839182"; "16e07"; "134985.0"; "-12034" |]

for value in values do
    try
        let number = UInt32.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
//       2147483648 --> 2147483648
//       14065839182: Overflow
//       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", "2147483648",  
                           "14065839182", "16e07", "134985.0", "-12034" }
For Each value As String In values
   Try
      Dim number As UInteger = UInt32.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
'       2147483648 --> 2147483648
'       14065839182: Overflow
'       16e07: Bad Format
'       134985.0: Bad Format
'       -12034: Overflow

備註

s 參數應該是下列格式數位的字串表示法。

[ws][符號]數位[ws]

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

元素 描述
ws 選擇性的空格符。
簽署 選擇性符號。 有效的符號字元取決於目前文化特性的 NumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign 屬性。 不過,負號符號只能搭配零使用;否則,方法會擲回 OverflowException
位數 範圍從 0 到 9 的數位序列。 會忽略任何前置零。

注意

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

s 參數是使用目前系統文化特性初始化之 System.Globalization.NumberFormatInfo 物件中的格式資訊來剖析。 如需詳細資訊,請參閱 NumberFormatInfo.CurrentInfo。 若要使用特定文化特性的格式資訊剖析字串,請使用 Parse(String, IFormatProvider) 方法。

另請參閱

適用於

Parse(String, NumberStyles)

來源:
UInt32.cs
來源:
UInt32.cs
來源:
UInt32.cs

重要

此 API 不符合 CLS 規範。

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

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

public:
 static System::UInt32 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static uint Parse (string s, System.Globalization.NumberStyles style);
public static uint Parse (string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint32
static member Parse : string * System.Globalization.NumberStyles -> uint32
Public Shared Function Parse (s As String, style As NumberStyles) As UInteger

參數

s
String

字串,表示要轉換的數位。 字串是使用 style 參數所指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,指定 s允許的格式。 要指定的一般值是 Integer

傳回

s中指定的數位相等的32位無符號整數。

屬性

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style不相容。

s 代表小於 UInt32.MinValue 或大於 UInt32.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 {
               uint number = UInt32.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 = UInt32.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 UInteger = UInt32.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 列舉的位旗標組合。 當 s 包含十六進位值的字串表示、s 所表示的數字系統(十進位或十六進位)只有在運行時間才知道,或當您想要不允許空格符或 s中的符號符號時,style 參數會讓這個方法多載很有用。

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

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

方括弧 ([ 和 ]) 中的元素是選擇性的。 如果 style 包含 NumberStyles.AllowHexSpecifiers 參數可能包含下列元素:

[ws]hexdigits[ws]

下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則空格符會顯示在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前的文化特性貨幣符號可能會出現在 s 中。
簽署 選擇性符號。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號會顯示在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。 不過,負號符號只能搭配零使用;否則,方法會擲回 OverflowException
位數

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 若為 fractional_digits,則只有數位0有效。
特定文化特性的群組分隔符符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的群組分隔符可能會出現在 s 中。
特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。 只有數位 0 可以顯示為小數位數,剖析作業才會成功;如果 fractional_digits 包含任何其他數位,則會擲回 FormatException
E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論 style 自變數的值為何,剖析作業都會忽略 s 中任何終止的 NUL (U+0000) 字元。

只有數位的字串(對應至 NumberStyles.None 樣式),如果字串位於 UInt32 類型的範圍,則一律會成功剖析。 其餘大部分 NumberStyles 成員控件元素,這些元素可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

NumberStyles 除了數位以外,s 中允許的專案
None 位數 元素。
AllowDecimalPoint 小數點 (.) 和 小數位數 元素。
AllowExponent “e” 或 “E” 字元,表示指數表示法,以及 exponential_digits
AllowLeadingWhite s開頭的 ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign s開頭的 符號 專案。
AllowTrailingSign s結尾處的 符號 專案。
AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
AllowThousands 群組分隔符 (,) 專案。
AllowCurrencySymbol currency ($) 元素。
Currency 所有元素。 不過,s 不能代表十六進位數或指數表示法的數位。
Float ws 項目位於 s的開頭或結尾,符號s開頭,以及小數點 (.) 符號。 s 參數也可以使用指數表示法。
Number wssign、群組分隔符()和小數點(.) 元素。
Any 所有元素。 不過,s 不能代表十六進位數。

與其他允許但不需要的其他 NumberStyles 值不同,s中特定樣式元素的存在,NumberStyles.AllowHexSpecifier 樣式值表示 s 中的個別數值字元一律會解譯為十六進位字元。 有效的十六進位字元為0-9、A-F和 a-f。 不允許前置詞,例如 “0x”。 唯一可以與 style 參數結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉包含包含兩個空格符旗標的複合數字樣式 NumberStyles.HexNumber

唯一可以與 style 參數結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉包含包含兩個空格符旗標的複合數字樣式 NumberStyles.HexNumber

注意

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

s 參數是使用目前系統文化特性初始化之 NumberFormatInfo 物件中的格式資訊來剖析。 若要指定格式資訊用於剖析作業的文化特性,請呼叫 Parse(String, NumberStyles, IFormatProvider) 多載。

另請參閱

適用於