UInt32.Parse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效 32 位无符号整数。
重载
Parse(String, NumberStyles, IFormatProvider) |
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 32 位无符号整数。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 32 位无符号整数。 |
Parse(String, NumberStyles) |
将指定样式的数字的字符串表示形式转换为它的等效 32 位无符号整数。 |
Parse(String) |
将数字的字符串表示形式转换为它的等效 32 位无符号整数。 |
Parse(String, IFormatProvider) |
将指定区域性特定格式的数字的字符串表示形式转换为它的等效 32 位无符号整数。 |
Parse(String, NumberStyles, IFormatProvider)
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 32 位无符号整数。
public:
static System::UInt32 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
[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 位无符号整数。
- 属性
例外
s
为 null
。
s
的格式不符合 style
。
示例
下面的示例使用 方法将数字的各种字符串表示形式转换为 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.
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]
方括号 ([ and ]) 中的元素是可选的。 如果 style
包含 NumberStyles.AllowHexSpecifier ,则 s
参数可能包含以下元素:
[ws]hexdigits[ws]
下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果 包含 标志,则空格可以出现在 的开头,如果 包含 标志,则它可以 s style 出现在 NumberStyles.AllowLeadingWhite s style 的 NumberStyles.AllowTrailingWhite 末尾。 |
$ | 区域性特定的货币符号。 其在字符串中的位置由 参数的 方法返回的对象的 CurrencyPositivePattern NumberFormatInfo GetFormat provider 属性定义。 如果 包含 标志, s 则货币 style 符号可以 NumberStyles.AllowCurrencySymbol 出现在 中。 |
sign | 可选符号。 (如果 包含负号并表示非零数字,则方法将引发 。) 如果 包含 标志,则 符号可以出现在 的开头,如果 包含 标志,则它可能会出现在 的 OverflowException s s style NumberStyles.AllowLeadingSign 末尾 s style NumberStyles.AllowTrailingSign 。 如果 包含 标志,则 括号可用于指示 s style 负 NumberStyles.AllowParentheses 值。 |
位数 | 从 0 到 9 的数字序列。 |
. | 区域性特定的小数点符号。 如果 包含 标志,则当前区域性的小数 s 点符号 style 可以 NumberStyles.AllowDecimalPoint 出现在 中。 |
fractional_digits | 数字 0-9 的一个或多个匹配项(如果 包含 标志)或数字 0 的一个或多个匹配项( style NumberStyles.AllowExponent 如果不包含)。 只有在包含 标志时, s 小数 style 位数才能出现在 NumberStyles.AllowDecimalPoint 中。 |
E | "e"或"E"字符,指示该值以指数表示法 (科学) 表示。 如果 包含 标志,参数可以表示指数 s style 表示 NumberStyles.AllowExponent 法中的数字。 |
exponential_digits | 从 0 到 9 的数字序列。 如果 包含 标志,参数可以表示指数 s style 表示 NumberStyles.AllowExponent 法中的数字。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
备注
分析操作将忽略 (U+0000) 的任何终止 NUL 字符,而不考虑 s
参数 style
的值。
只有十进制数字的 (字符串,它对应于样式) NumberStyles.None 始终成功分析。 其余大多数成员控制元素,这些元素可能存在于此输入字符串中,但不一定 NumberStyles 存在。 下表指示各个成员 NumberStyles 如何影响 中可能存在的元素 s
。
非复合 NumberStyles 值 |
除了数字之外 s ,还允许使用 元素 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | 小数点 (.) 和 fractional_digits 元素。 但是,如果样式不包含 标志,则fractional_digits必须只包含一个或多个 NumberStyles.AllowExponent 0 位数字;否则将引发 OverflowException 。 |
NumberStyles.AllowExponent | "e"或"E"字符,指示指数表示法以及 exponential_digits。 |
NumberStyles.AllowLeadingWhite | 开头的 ws 元素 s 。 |
NumberStyles.AllowTrailingWhite | 末尾的 ws 元素 s 。 |
NumberStyles.AllowLeadingSign | 数字 前的 符号。 |
NumberStyles.AllowTrailingSign | 数字 后的 符号。 |
NumberStyles.AllowParentheses | 在数字之前和之后 用括号表示 负值。 |
NumberStyles.AllowThousands | 组分隔符 (,) 元素。 |
NumberStyles.AllowCurrencySymbol | 元素 $ () 货币。 |
如果使用 NumberStyles.AllowHexSpecifier 标志, s
必须是十六进制值。 唯一可以与它组合的其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 (NumberStyles 枚举包括一个包含空白 NumberStyles.HexNumber flags.) 的复合数字样式 。
备注
如果 参数是十六进制数的字符串表示形式,则它前面不能带有任何修饰 (如 或) 将其区分为十六进制数字 s
0x
&h
。 这将导致分析操作引发异常。
参数 provider
是一 IFormatProvider 个实现,其 GetFormat 方法返回 NumberFormatInfo 一个 对象,该对象提供有关 的格式的区域性特定信息 s
。 有三种方法可以使用 provider
参数向分析操作提供自定义格式设置信息:
可以传递提供 NumberFormatInfo 格式设置信息的实际对象。 (它的 实现 GetFormat 仅返回自身.)
可以传递 CultureInfo 一个 对象,该对象指定要使用其格式的区域性。 其 NumberFormat 属性提供格式设置信息。
可以传递自定义 IFormatProvider 实现。 其 GetFormat 方法必须实例化并返回 NumberFormatInfo 提供格式设置信息的对象。
如果 provider
null
为 , NumberFormatInfo 则使用当前区域性的 对象。
另请参阅
适用于
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
重要
此 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(String, NumberStyles)
将指定样式的数字的字符串表示形式转换为它的等效 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 位无符号整数。
- 属性
例外
s
为 null
。
s
的格式不符合 style
。
示例
下面的示例尝试使用多个值分析字符串数组中的每个元素 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
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 。 当 style
s
包含十六进制值的字符串表示形式时,如果仅在运行时才知道由表示的数字系统 (decimal 或十六进制) s
,或者当你想要禁止在中使用空格或符号时,参数会使此方法重载有用 s
。
根据的值 style
, s
参数可能包括以下元素:
[ws][ $ ] [sign] [数字,]位数[。fractional_digits] [E [sign]exponential_digits] [ws]
方括号 ([ and ]) 中的元素是可选的。 如果 style
包含 NumberStyles.AllowHexSpecifier ,则 s
参数可能包含以下元素:
[ws]hexdigits[ws]
下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空白。 如果包含标志,则空格可以出现在的开头 s style NumberStyles.AllowLeadingWhite ,如果包含标志,则可以在的末尾出现空白 s style NumberStyles.AllowTrailingWhite 。 |
$ | 区域性特定的货币符号。 其在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern NumberFormatInfo.CurrencyPositivePattern 当前区域性的和属性定义。 s 如果包含标志,则当前区域性的货币符号可以出现在中 style NumberStyles.AllowCurrencySymbol 。 |
sign | 一个可选的符号。 如果包含标志,则符号可以出现在的开头 s style NumberStyles.AllowLeadingSign ,如果包含标志,则它可以出现在的结尾 s style NumberStyles.AllowTrailingSign 。 如果包含标志,则可以在中使用括号 s 来表示负值 style NumberStyles.AllowParentheses 。 但负号符号只能与零一起使用;否则,该方法将引发 OverflowException 。 |
位数 fractional_digits exponential_digits |
介于0到9之间的一系列数字。 对于 fractional_digits,只有数字0有效。 |
, | 区域性特定的组分隔符符号。 s 如果包含标志,则当前区域性的组分隔符可以出现在中 style NumberStyles.AllowThousands 。 |
. | 区域性特定的小数点符号。 s 如果包含标志,则当前区域性的小数点符号可以出现在中 style NumberStyles.AllowDecimalPoint 。 只有数字0才能作为小数位出现,以便分析操作成功;如果 fractional_digits 包含任何其他数字, FormatException 则会引发。 |
E | "E" 或 "E" 字符,指示以指数 (科学) 记数法表示的值。 s 如果包含标志,则参数可以表示指数表示法中的数字 style NumberStyles.AllowExponent 。 |
hexdigits | 从0到 f 的十六进制数字序列,或者从0到 F。 |
备注
s
无论参数的值如何,分析操作都将忽略中任何终止的 NUL (U + 0000) 字符 style
。
只包含数字的字符串 (与 NumberStyles.None 样式) 始终在类型范围内分析成功 UInt32 。 大多数剩余 NumberStyles 成员控件可能存在但不需要存在于输入字符串中的元素。 下表指示各个成员如何 NumberStyles 影响可能出现在中的元素 s
。
NumberStyles 值 |
中允许的元素 s 以及数字 |
---|---|
None | 仅限 数字 元素。 |
AllowDecimalPoint | 小数点 ( ) 和 小数位数 元素。 |
AllowExponent | "E" 或 "E" 字符(指示指数表示法)以及 exponential_digits。 |
AllowLeadingWhite | 开头的 ws 元素 s 。 |
AllowTrailingWhite | 末尾处的 ws 元素 s 。 |
AllowLeadingSign | 开头的 sign 元素 s 。 |
AllowTrailingSign | 末尾的 sign 元素 s 。 |
AllowParentheses | 用括号括起数值的 符号 元素。 |
AllowThousands | 组分隔符 (,) 元素。 |
AllowCurrencySymbol | 货币 ($) 元素。 |
Currency | 所有元素。 但是, s 不能表示十六进制数或以指数表示法表示的数字。 |
Float | 开头或结尾处的 ws 元素,在 s 的开头处进行 签名, s 小数点 () 符号。 s 参数还可以使用指数表示法。 |
Number | ws 、 sign 、组分隔符 (、) 和小数点 () 元素。 |
Any | 所有元素。 但是, s 不能表示十六进制数。 |
与其他值不同, NumberStyles 它们允许但不要求在中存在特定样式元素 s
, NumberStyles.AllowHexSpecifier 样式值意味着中的单个数字字符 s
始终解释为十六进制字符。 有效的十六进制字符为0-9、A-f 和 a-f。 不允许使用前缀,如 "0x"。 只能与参数组合的其他标志 style
是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 枚举 (NumberStyles 包含复合数字样式, NumberStyles.HexNumber 其中包含两个空白标志。 )
只能与参数组合的其他标志 style
是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 枚举 (NumberStyles 包含复合数字样式, NumberStyles.HexNumber 其中包含两个空白标志。 )
备注
如果 s
是十六进制数的字符串表示形式,则它的前面不能有任何修饰 (例如 0x
或 &h
) ,它将其视为十六进制数。 这将导致转换失败。
s
通过使用为 NumberFormatInfo 当前系统区域性初始化的对象中的格式设置信息来分析参数。 若要指定其格式设置信息用于分析操作的区域性,请调用 Parse(String, NumberStyles, IFormatProvider) 重载。
另请参阅
适用于
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
一个字符串,表示要转换的数字。
返回
与 s
中包含的数字等效的 32 位无符号整数。
- 属性
例外
s
参数为 null
。
参数 s
的格式不正确。
示例
下面的示例使用 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
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][符号]digits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 有效符号字符由当前 NumberFormatInfo.NegativeSign 区域性的 NumberFormatInfo.PositiveSign 和 属性确定。 但是,负号符号只能与零一起使用;否则, 方法将引发 OverflowException 。 |
位数 | 从 0 到 9 的数字序列。 将忽略任何前导零。 |
备注
由参数指定的字符串 s
使用样式来进行解释 NumberStyles.Integer 。 它不能包含任何组分隔符或小数分隔符,也不能包含小数部分。
s
通过使用为 System.Globalization.NumberFormatInfo 当前系统区域性初始化的对象中的格式设置信息来分析参数。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要通过使用特定区域性的格式设置信息分析字符串,请使用 Parse(String, IFormatProvider) 方法。
另请参阅
适用于
Parse(String, IFormatProvider)
将指定区域性特定格式的数字的字符串表示形式转换为它的等效 32 位无符号整数。
public:
static System::UInt32 Parse(System::String ^ s, IFormatProvider ^ provider);
[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
为 null
。
s
的样式不正确。
示例
以下示例是 Web 窗体的按钮单击事件处理程序。 它使用 属性返回的 HttpRequest.UserLanguages 数组来确定用户区域设置。 然后,它实例 CultureInfo 化对应于该区域设置的对象。 然后 NumberFormatInfo ,将属于该对象的对象传递给 方法,以将 CultureInfo 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][符号]digits[ws]
[和 ] (方括号中的) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号;如果 表示值零, s 则返回负号。 |
位数 | 从 0 到 9 的数字序列。 |
使用 样式解释 s NumberStyles.Integer 参数。 除了无符号整数值的十进制数字之外,仅允许前导空格和尾随空格以及前导符号。 (如果存在负号, 必须表示零值,或者 方法引发 .) 若要显式定义样式元素以及可存在于 中的区域性特定格式设置信息,请使用 s
OverflowException s
Parse(String, NumberStyles, IFormatProvider) 方法。
参数 provider
是一 IFormatProvider 个实现,其 GetFormat 方法返回 NumberFormatInfo 一个 对象,该对象提供有关 的格式的区域性特定信息 s
。 有三种方法可以使用 provider
参数向分析操作提供自定义格式设置信息:
可以传递提供 NumberFormatInfo 格式设置信息的实际对象。 (它的 实现 GetFormat 仅返回自身.)
可以传递 CultureInfo 一个 对象,该对象指定要使用其格式的区域性。 其 NumberFormat 属性提供格式设置信息。
可以传递自定义 IFormatProvider 实现。 其 GetFormat 方法必须实例化并返回 NumberFormatInfo 提供格式设置信息的对象。
如果 provider
null
为 , NumberFormatInfo 则使用当前区域性的 。