Int32.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 int Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public static int Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static int Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Integer
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,用于提供有关 s
格式的区域性特定信息。
返回
与 s
中指定的数字等效的 32 位带符号整数。
实现
例外
s
为 null
。
s
的格式不符合 style
。
示例
下面的示例使用各种 style
参数 provider
来分析值的字符串表示形式 Int32 。 它还说明了可以解释相同字符串的某些不同方式,具体取决于其格式信息用于分析操作的区域性。
using namespace System;
using namespace System::Globalization;
public ref class ParseInt32
{
public:
static void Main()
{
Convert("12,000", NumberStyles::Float | NumberStyles::AllowThousands,
gcnew CultureInfo("en-GB"));
Convert("12,000", NumberStyles::Float | NumberStyles::AllowThousands,
gcnew CultureInfo("fr-FR"));
Convert("12,000", NumberStyles::Float, gcnew CultureInfo("en-US"));
Convert("12 425,00", NumberStyles::Float | NumberStyles::AllowThousands,
gcnew CultureInfo("sv-SE"));
Convert("12,425.00", NumberStyles::Float | NumberStyles::AllowThousands,
NumberFormatInfo::InvariantInfo);
Convert("631,900", NumberStyles::Integer | NumberStyles::AllowDecimalPoint,
gcnew CultureInfo("fr-FR"));
Convert("631,900", NumberStyles::Integer | NumberStyles::AllowDecimalPoint,
gcnew CultureInfo("en-US"));
Convert("631,900", NumberStyles::Integer | NumberStyles::AllowThousands,
gcnew CultureInfo("en-US"));
}
private:
static void Convert(String^ value, NumberStyles style,
IFormatProvider^ provider)
{
try
{
int number = Int32::Parse(value, style, provider);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException^)
{
Console::WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException^)
{
Console::WriteLine("'{0}' is out of range of the Int32 type.", value);
}
}
};
int main()
{
ParseInt32::Main();
}
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int32 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
using System;
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("en-GB"));
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("fr-FR"));
Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));
Convert("12 425,00", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("sv-SE"));
Convert("12,425.00", NumberStyles.Float | NumberStyles.AllowThousands,
NumberFormatInfo.InvariantInfo);
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("fr-FR"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowThousands,
new CultureInfo("en-US"));
}
private static void Convert(string value, NumberStyles style,
IFormatProvider provider)
{
try
{
int number = Int32.Parse(value, style, provider);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int32 type.", value);
}
}
}
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int32 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
open System
open System.Globalization
let convert (value: string) (style: NumberStyles) (provider: IFormatProvider) =
try
let number = Int32.Parse(value, style, provider)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int32 type."
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "en-GB")
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "fr-FR")
convert "12,000" NumberStyles.Float (CultureInfo "en-US")
convert "12 425,00" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "sv-SE")
convert "12,425.00" (NumberStyles.Float ||| NumberStyles.AllowThousands) NumberFormatInfo.InvariantInfo
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "fr-FR")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "en-US")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowThousands) (CultureInfo "en-US")
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int32 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
Imports System.Globalization
Module ParseInt32
Public Sub Main()
Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("en-GB"))
Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("fr-FR"))
Convert("12,000", NumberStyles.Float, New CultureInfo("en-US"))
Convert("12 425,00", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("sv-SE"))
Convert("12,425.00", NumberStyles.Float Or NumberStyles.AllowThousands, _
NumberFormatInfo.InvariantInfo)
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
New CultureInfo("fr-FR"))
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
New CultureInfo("en-US"))
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowThousands, _
New CultureInfo("en-US"))
End Sub
Private Sub Convert(value As String, style As NumberStyles, _
provider As IFormatProvider)
Try
Dim number As Integer = Int32.Parse(value, style, provider)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int32 type.", value)
End Try
End Sub
End Module
' This example displays the following output to the console:
' Converted '12,000' to 12000.
' Converted '12,000' to 12.
' Unable to convert '12,000'.
' Converted '12 425,00' to 12425.
' Converted '12,425.00' to 12425.
' '631,900' is out of range of the Int32 type.
' Unable to convert '631,900'.
' Converted '631,900' to 631900.
注解
该 style
参数定义样式元素 (,例如空格或正符号) ,允许在参数中 s
成功分析操作。 它必须是枚举中的 NumberStyles 位标志的组合。 根据值 style
, s
参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional_digist][e[sign]exponential_digits][ws]
或者,如果 style
包括 AllowHexSpecifier:
[ws]hexdigits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果包含标志,则空白可以显示在标志的s``style 开头,如果style 包含标志,NumberStyles.AllowTrailingWhite则它可能显示在末尾s 。NumberStyles.AllowLeadingWhite |
$ | 特定于区域性的货币符号。 字符串中的位置由NumberFormatInfo.CurrencyPositivePattern参数方法provider 返回GetFormat的对象的属性NumberFormatInfo定义。 如果style 包含标志,NumberStyles.AllowCurrencySymbol则可以显示s 货币符号。 |
sign | 可选符号。 如果包含标志或NumberStyles.AllowLeadingSign标志的NumberStyles.AllowTrailingSign style s 末尾,则标志可能显示在开头s``style 。 如果style 包含NumberStyles.AllowParentheses标志,可以使用s 括号来指示负值。 |
位数 fractional_digits exponential_digits |
从 0 到 9 的数字序列。 对于 fractional_digits,只有数字 0 有效。 |
, | 区域性特定的千位分隔符符号。 如果包含NumberStyles.AllowThousands标志,则可以显示style``s 指定的provider 区域性的千位分隔符。 |
. | 区域性特定的小数点符号。 如果包含NumberStyles.AllowDecimalPoint标志,则可以显示style``s 由指定的provider 区域性的小数点符号。只有数字 0 才能显示为分析操作成功的小数位数;如果 fractional_digits 包含任何其他数字,则会引发一个 OverflowException 。 |
e | “e”或“E”字符,指示该值以指数表示法表示。 如果style 包含标志,则s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
备注
无论参数的值style
如何,分析操作都会忽略任何终止 NUL (U+0000) 字符s
。
只有十进制数字的字符串 (,它与 NumberStyles.None 样式) 在类型范围内 Int32 时始终成功分析。 大多数剩余 NumberStyles 成员控制元素可能不是此输入字符串中的必需元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s
的元素。
非复合 NumberStyles 值 | 除数字外允许的元素 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | 小数点 ( 。 ) 和 小数位数 元素。 但是, 小数位数 必须仅包含一个或多个 0 位或引发数字 OverflowException 。 |
NumberStyles.AllowExponent | 该 s 参数还可以使用指数表示法。 如果 s 表示指数表示法中的数字,则必须在数据类型范围内 Int32 表示一个整数,而不使用非零小数分量。 |
NumberStyles.AllowLeadingWhite | 开头的 s ws 元素。 |
NumberStyles.AllowTrailingWhite | 末尾的 s ws 元素 |
NumberStyles.AllowLeadingSign | 正号可以在 数字 之前显示。 |
NumberStyles.AllowTrailingSign | 正号可以在 数字 之后显示。 |
NumberStyles.AllowParentheses | 括住数值的括号形式的 符号 元素。 |
NumberStyles.AllowThousands | 千位分隔符 ( , ) 元素。 |
NumberStyles.AllowCurrencySymbol | 元素 $ 。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
则必须是没有前缀的十六进制值。 例如,“C9AF3”已成功分析,但“0xC9AF3”不会。 可以存在于 style
其中的唯一其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 NumberStyles (枚举具有复合数字样式,NumberStyles.HexNumber其中包含两个空格标志。)
参数 provider
是实现 IFormatProvider ,例如 NumberFormatInfo 或 CultureInfo 对象。 该 provider
参数提供分析中使用的区域性特定信息。 null
如果是provider
,NumberFormatInfo则使用当前区域性的对象。
另请参阅
适用于
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 32 位带符号整数。
public static int Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static int Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Integer
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,用于提供有关 s
格式的区域性特定信息。
返回
与 s
中指定的数字等效的 32 位带符号整数。
实现
适用于
Parse(String, NumberStyles)
将指定样式的数字的字符串表示形式转换为它的等效 32 位有符号整数。
public:
static int Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static int Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> int
Public Shared Function Parse (s As String, style As NumberStyles) As Integer
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
返回
与 s
中指定的数字等效的 32 位带符号整数。
例外
s
为 null
。
s
的格式不符合 style
。
示例
以下示例使用 Int32.Parse(String, NumberStyles) 该方法分析多个 Int32 值的字符串表示形式。 此示例的当前区域性为 en-US。
using namespace System;
using namespace System::Globalization;
public ref class ParseInt32
{
public:
static void Main()
{
Convert("104.0", NumberStyles::AllowDecimalPoint);
Convert("104.9", NumberStyles::AllowDecimalPoint);
Convert(" $17,198,064.42", NumberStyles::AllowCurrencySymbol |
NumberStyles::Number);
Convert("103E06", NumberStyles::AllowExponent);
Convert("-1,345,791", NumberStyles::AllowThousands);
Convert("(1,345,791)", NumberStyles::AllowThousands |
NumberStyles::AllowParentheses);
}
private:
static void Convert(String^ value, NumberStyles style)
{
try
{
int number = Int32::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException^)
{
Console::WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException^)
{
Console::WriteLine("'{0}' is out of range of the Int32 type.", value);
}
}
};
int main()
{
ParseInt32::Main();
}
// The example displays the following output to the console:
// Converted '104.0' to 104.
// '104.9' is out of range of the Int32 type.
// ' $17,198,064.42' is out of range of the Int32 type.
// Converted '103E06' to 103000000.
// Unable to convert '-1,345,791'.
// Converted '(1,345,791)' to -1345791.
using System;
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("104.0", NumberStyles.AllowDecimalPoint);
Convert("104.9", NumberStyles.AllowDecimalPoint);
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert("103E06", NumberStyles.AllowExponent);
Convert("-1,345,791", NumberStyles.AllowThousands);
Convert("(1,345,791)", NumberStyles.AllowThousands |
NumberStyles.AllowParentheses);
}
private static void Convert(string value, NumberStyles style)
{
try
{
int number = Int32.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int32 type.", value);
}
}
}
// The example displays the following output to the console:
// Converted '104.0' to 104.
// '104.9' is out of range of the Int32 type.
// ' $17,198,064.42' is out of range of the Int32 type.
// Converted '103E06' to 103000000.
// Unable to convert '-1,345,791'.
// Converted '(1,345,791)' to -1345791.
open System
open System.Globalization
let convert value (style: NumberStyles) =
try
let number = Int32.Parse(value, style)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int32 type."
convert "104.0" NumberStyles.AllowDecimalPoint
convert "104.9" NumberStyles.AllowDecimalPoint
convert " $17,198,064.42" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert "103E06" NumberStyles.AllowExponent
convert "-1,345,791" NumberStyles.AllowThousands
convert "(1,345,791)" (NumberStyles.AllowThousands ||| NumberStyles.AllowParentheses)
// The example displays the following output to the console:
// Converted '104.0' to 104.
// '104.9' is out of range of the Int32 type.
// ' $17,198,064.42' is out of range of the Int32 type.
// Converted '103E06' to 103000000.
// Unable to convert '-1,345,791'.
// Converted '(1,345,791)' to -1345791.
Imports System.Globalization
Module ParseInt32
Public Sub Main()
Convert("104.0", NumberStyles.AllowDecimalPoint)
Convert("104.9", NumberStyles.AllowDecimalPoint)
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol Or _
NumberStyles.Number)
Convert("103E06", NumberStyles.AllowExponent)
Convert("-1,345,791", NumberStyles.AllowThousands)
Convert("(1,345,791)", NumberStyles.AllowThousands Or _
NumberStyles.AllowParentheses)
End Sub
Private Sub Convert(value As String, style As NumberStyles)
Try
Dim number As Integer = Int32.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int32 type.", value)
End Try
End Sub
End Module
' The example displays the following output to the console:
' Converted '104.0' to 104.
' '104.9' is out of range of the Int32 type.
' ' $17,198,064.42' is out of range of the Int32 type.
' Converted '103E06' to 103000000.
' Unable to convert '-1,345,791'.
' Converted '(1,345,791)' to -1345791.
注解
该 style
参数定义样式元素 (,例如空格、正符号或负号符号,或允许在参数中 s
允许分析操作成功的千位分隔符符号) 。 它必须是枚举中的 NumberStyles 位标志的组合。 根据其值 style
,参数 s
可能包含以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]exponential_digits][ws]
或者,如果 style
包括 AllowHexSpecifier:
[ws]hexdigits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果style 包含标志,则空白可以出现在标志的开头s ,如果style 包含NumberStyles.AllowTrailingWhite标志,则显示在该标志的s NumberStyles.AllowLeadingWhite末尾。 |
$ | 特定于区域性的货币符号。 字符串中的位置由NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern当前区域性的属性定义。 如果包含标志,则当前区域性的货币符号可以出现在其中 s style NumberStyles.AllowCurrencySymbol 。 |
sign | 可选符号。 如果style 包含标志,则符号可以出现在标志的开头s ,如果style 包含NumberStyles.AllowTrailingSign标志,则它将显示在标志的s NumberStyles.AllowLeadingSign末尾。 如果包含NumberStyles.AllowParentheses标志,可以使用s 括号来指示负值style 。 |
位数 fractional_digits exponential_digits |
0 到 9 的一系列数字。 对于 fractional_digits,只有数字 0 有效。 |
, | 区域性特定的千位分隔符。 如果包含标志,则当前区域性的千位分隔符可以出现在s``style 其中NumberStyles.AllowThousands。 |
. | 特定于区域性的小数点符号。 如果包含标志,则当前区域性的小数点符号可以出现在s``style 其中NumberStyles.AllowDecimalPoint。 只有数字 0 才能显示为分析操作成功的小数位数;如果 fractional_digits 包含任何其他数字,则会引发一个 OverflowException 。 |
e | “e”或“E”字符,指示该值以指数表示法表示。 如果style 包含标志,参数s 可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
备注
无论参数的值style
如何,分析操作都会忽略任何终止 NUL (U+0000) 字符s
。
仅包含数字的字符串 (,它对应于 NumberStyles.None 样式) 在类型范围内 Int32 时始终分析成功。 大多数剩余 NumberStyles 成员控制可能但不需要出现在输入字符串中的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于的 s
元素。
NumberStyles 值 | 除数字外允许的元素 |
---|---|
None | 仅 数字 元素。 |
AllowDecimalPoint | 小数点 ( 。 ) 和 小数位数 元素。 |
AllowExponent | 该 s 参数还可以使用指数表示法。 |
AllowLeadingWhite | 开头的 ws 元素s 。 |
AllowTrailingWhite | 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的 s sign 元素。 |
AllowTrailingSign | 结尾处的 s sign 元素 |
AllowParentheses | 括住数值的括号形式的 符号 元素。 |
AllowThousands | 千位分隔符 (,) 元素。 |
AllowCurrencySymbol | 元素 $ 。 |
Currency | 全部。 参数 s 不能表示十六进制数或指数表示法中的数字。 |
Float | 开头或结尾处的 s ws 元素,符号 位于开头s ,小数点 (。 ) 符号。 该 s 参数还可以使用指数表示法。 |
Number | 千位分隔符 (、) 和小数点 (。 ws``sign ) 元素。 |
Any | 除不能表示十六进制数外 s 的所有样式。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
则必须是没有前缀的十六进制值。 例如,“C9AF3”已成功分析,但“0xC9AF3”不会。 唯一可以与 s
参数组合的其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 NumberStyles (枚举包含复合数字样式,NumberStyles.HexNumber包括空格标志.)
该 s
参数使用为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息进行分析。 若要指定其格式设置信息用于分析操作的区域性,请调用 Int32.Parse(String, NumberStyles, IFormatProvider) 重载。
另请参阅
适用于
Parse(String)
将数字的字符串表示形式转换为它的等效 32 位有符号整数。
public:
static int Parse(System::String ^ s);
public static int Parse (string s);
static member Parse : string -> int
Public Shared Function Parse (s As String) As Integer
参数
- s
- String
包含要转换的数字的字符串。
返回
与 s
中包含的数字等效的 32 位有符号整数。
例外
s
为 null
。
s
的格式不正确。
示例
以下示例演示如何使用 Int32.Parse(String) 该方法将字符串值转换为 32 位带符号整数值。 然后,生成的整数值会显示到控制台。
using namespace System;
void main()
{
array<String^>^ values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "007", "2147483647",
"2147483648", "16e07", "134985.0", "-12034",
"-2147483648", "-2147483649" };
for each (String^ value in values)
{
try {
Int32 number = Int32::Parse(value);
Console::WriteLine("{0} --> {1}", value, number);
}
catch (FormatException^ e) {
Console::WriteLine("{0}: Bad Format", value);
}
catch (OverflowException^ e) {
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 --> -10
// 007 --> 7
// 2147483647 --> 2147483647
// 2147483648: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034 --> -12034
// -2147483648 --> -2147483648
// -2147483649: Overflow
using System;
public class Example
{
public static void Main()
{
string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "007", "2147483647",
"2147483648", "16e07", "134985.0", "-12034",
"-2147483648", "-2147483649" };
foreach (string value in values)
{
try {
int number = Int32.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 --> -10
// 007 --> 7
// 2147483647 --> 2147483647
// 2147483648: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034 --> -12034
// -2147483648 --> -2147483648
// -2147483649: Overflow
open System
let values =
[ "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
"0xFA1B"; "163042"; "-10"; "007"; "2147483647"
"2147483648"; "16e07"; "134985.0"; "-12034"
"-2147483648"; "-2147483649" ]
for value in values do
try
let number = Int32.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 --> -10
// 007 --> 7
// 2147483647 --> 2147483647
// 2147483648: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034 --> -12034
// -2147483648 --> -2147483648
// -2147483649: Overflow
Module Example
Public Sub Main()
Dim values() As String = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "007", "2147483647",
"2147483648", "16e07", "134985.0", "-12034",
"-2147483648", "-2147483649" }
For Each value As String In values
Try
Dim number As Integer = Int32.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
End Sub
End Module
' 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 --> -10
' 007 --> 7
' 2147483647 --> 2147483647
' 2147483648: Overflow
' 16e07: Bad Format
' 134985.0: Bad Format
' -12034 --> -12034
' -2147483648 --> -2147483648
' -2147483649: Overflow
注解
该 s
参数包含多种形式:
[ws][sign]digits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
签名 | 可选符号。 |
位数 | 一系列数字,范围从 0 到 9。 |
参数 s
使用 NumberStyles.Integer 样式进行解释。 除了十进制数字以外,还允许前导和尾随空格以及前导符号。 若要显式定义可以存在的 s
样式元素,请使用 Int32.Parse(String, NumberStyles) 或 Int32.Parse(String, NumberStyles, IFormatProvider) 方法。
该 s
参数使用为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息进行分析。 有关详细信息,请参阅 CurrentInfo。 若要使用其他区域性的格式设置信息分析字符串,请使用 Int32.Parse(String, NumberStyles, IFormatProvider) 该方法。
另请参阅
适用于
Parse(String, IFormatProvider)
将指定的区域性特定格式的数字的字符串表示形式转换为它的等效 32 位有符号整数。
public:
static int Parse(System::String ^ s, IFormatProvider ^ provider);
public static int Parse (string s, IFormatProvider provider);
public static int Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> int
Public Shared Function Parse (s As String, provider As IFormatProvider) As Integer
参数
- s
- String
包含要转换的数字的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
与 s
中指定的数字等效的 32 位带符号整数。
实现
例外
s
为 null
。
s
的格式不正确。
示例
以下示例是 Web 窗体的按钮单击事件处理程序。 它使用属性返回 HttpRequest.UserLanguages 的数组来确定用户的区域设置。 然后,它会实例化 CultureInfo 对应于该区域设置的对象。 NumberFormatInfo然后,将属于该CultureInfo对象的对象传递给Parse(String, IFormatProvider)方法,将用户的输入转换为Int32值。
protected void OkToInteger_Click(object sender, EventArgs e)
{
string locale;
int 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 = Int32.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 OkToInteger_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToInteger.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Integer
' 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 = Int32.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
注解
此方法的 Parse(String, IFormatProvider) 此重载通常用于将各种格式 Int32 的文本转换为值。 例如,它可用于将用户输入的文本转换为 HTML 文本框中的数值。
该 s
参数包含多种形式:
[ws][sign]digits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
参数 s
使用 NumberStyles.Integer 样式进行解释。 除了十进制数字外,只允许前导和尾随空格以及前导符号。 若要显式定义可以存在的 s
样式元素,请使用 Int32.Parse(String, NumberStyles, IFormatProvider) 该方法。
参数 provider
是实现 IFormatProvider ,例如 NumberFormatInfo 或 CultureInfo 对象。 参数 provider
提供有关格式的 s
区域性特定信息。 null
如果是provider
,NumberFormatInfo则使用当前区域性的对象。