SByte.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數字的字串表示轉換成它的對等 8 位元帶正負號的整數。
多載
Parse(String, NumberStyles, IFormatProvider) |
將數字的字串表示 (使用指定的樣式和特定文化特性的格式) 轉換成它的對等 8 位元帶正負號的整數。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 8 位元帶正負號的整數。 |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
將 UTF-8 字元的範圍剖析為值。 |
Parse(String, IFormatProvider) |
將指定特定文化特性格式之數字的字串表示轉換成它的對等 8 位元帶正負號的整數。 |
Parse(String) |
將數字的字串表示轉換成它的對等 8 位元帶正負號的整數。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
將字元範圍剖析為值。 |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
將 UTF-8 字元的範圍剖析為值。 |
Parse(String, NumberStyles) |
將指定樣式之數字的字串表示轉換成它的對等 8 位元帶正負號的整數。 |
Parse(String, NumberStyles, IFormatProvider)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將數字的字串表示 (使用指定的樣式和特定文化特性的格式) 轉換成它的對等 8 位元帶正負號的整數。
public:
static System::SByte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static System::SByte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::SByte>::Parse;
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> sbyte
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> sbyte
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As SByte
參數
- s
- String
字串,包含要轉換的數字。 這個字串使用 style
指定的樣式來解譯。
- style
- NumberStyles
列舉值的位元組合,表示 s
中可以存在的樣式項目。 一般會指定的值是 Integer。
- provider
- IFormatProvider
物件,其提供關於 s
的特定文化特性格式資訊。 如果 provider
是 null
,則會使用執行緒目前的文化特性。
傳回
8 位元帶正負號的位元組值,等於 s
參數中所指定的數字。
實作
- 屬性
例外狀況
s
為 null
。
s
的格式與 style
不相容。
範例
下列範例說明如何使用 Parse(String, NumberStyles, IFormatProvider) 方法,將數位的各種字串表示轉換成帶正負號的整數值。
using System;
using System.Globalization;
public class SByteConversion
{
NumberFormatInfo provider = NumberFormatInfo.CurrentInfo;
public static void Main()
{
string stringValue;
NumberStyles style;
stringValue = " 123 ";
style = NumberStyles.None;
CallParseOperation(stringValue, style);
stringValue = "000,000,123";
style = NumberStyles.Integer | NumberStyles.AllowThousands;
CallParseOperation(stringValue, style);
stringValue = "-100";
style = NumberStyles.AllowLeadingSign;
CallParseOperation(stringValue, style);
stringValue = "100-";
style = NumberStyles.AllowLeadingSign;
CallParseOperation(stringValue, style);
stringValue = "100-";
style = NumberStyles.AllowTrailingSign;
CallParseOperation(stringValue, style);
stringValue = "$100";
style = NumberStyles.AllowCurrencySymbol;
CallParseOperation(stringValue, style);
style = NumberStyles.Integer;
CallParseOperation(stringValue, style);
style = NumberStyles.AllowDecimalPoint;
CallParseOperation("100.0", style);
stringValue = "1e02";
style = NumberStyles.AllowExponent;
CallParseOperation(stringValue, style);
stringValue = "(100)";
style = NumberStyles.AllowParentheses;
CallParseOperation(stringValue, style);
}
private static void CallParseOperation(string stringValue,
NumberStyles style)
{
sbyte number;
if (stringValue == null)
Console.WriteLine("Cannot parse a null string...");
try
{
number = sbyte.Parse(stringValue, style);
Console.WriteLine("SByte.Parse('{0}', {1})) = {2}",
stringValue, style, number);
}
catch (FormatException)
{
Console.WriteLine("'{0}' and {1} throw a FormatException",
stringValue, style);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is outside the range of a signed byte",
stringValue);
}
}
}
// The example displays the following information to the console:
// ' 123 ' and None throw a FormatException
// SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
// SByte.Parse('-100', AllowLeadingSign)) = -100
// '100-' and AllowLeadingSign throw a FormatException
// SByte.Parse('100-', AllowTrailingSign)) = -100
// SByte.Parse('$100', AllowCurrencySymbol)) = 100
// '$100' and Integer throw a FormatException
// SByte.Parse('100.0', AllowDecimalPoint)) = 100
// SByte.Parse('1e02', AllowExponent)) = 100
// SByte.Parse('(100)', AllowParentheses)) = -100
open System
open System.Globalization
let provider = NumberFormatInfo.CurrentInfo
let callParseOperation stringValue (style: NumberStyles) =
if stringValue = null then
printfn "Cannot parse a null string..."
else
try
let number = SByte.Parse(stringValue, style)
printfn $"SByte.Parse('{stringValue}', {style})) = {number}"
with
| :? FormatException ->
printfn $"'{stringValue}' and {style} throw a FormatException"
| :? OverflowException ->
printfn $"'{stringValue}' is outside the range of a signed byte"
[<EntryPoint>]
let main _ =
let stringValue = " 123 "
let style = NumberStyles.None
callParseOperation stringValue style
let stringValue = "000,000,123"
let style = NumberStyles.Integer ||| NumberStyles.AllowThousands
callParseOperation stringValue style
let stringValue = "-100"
let style = NumberStyles.AllowLeadingSign
callParseOperation stringValue style
let stringValue = "100-"
let style = NumberStyles.AllowLeadingSign
callParseOperation stringValue style
let stringValue = "100-"
let style = NumberStyles.AllowTrailingSign
callParseOperation stringValue style
let stringValue = "$100"
let style = NumberStyles.AllowCurrencySymbol
callParseOperation stringValue style
let style = NumberStyles.Integer
callParseOperation stringValue style
let style = NumberStyles.AllowDecimalPoint
callParseOperation "100.0" style
let stringValue = "1e02"
let style = NumberStyles.AllowExponent
callParseOperation stringValue style
let stringValue = "(100)"
let style = NumberStyles.AllowParentheses
callParseOperation stringValue style
0
// The example displays the following information to the console:
// ' 123 ' and None throw a FormatException
// SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
// SByte.Parse('-100', AllowLeadingSign)) = -100
// '100-' and AllowLeadingSign throw a FormatException
// SByte.Parse('100-', AllowTrailingSign)) = -100
// SByte.Parse('$100', AllowCurrencySymbol)) = 100
// '$100' and Integer throw a FormatException
// SByte.Parse('100.0', AllowDecimalPoint)) = 100
// SByte.Parse('1e02', AllowExponent)) = 100
// SByte.Parse('(100)', AllowParentheses)) = -100
Imports System.Globalization
Module modMain
Public Sub Main()
Dim byteString As String
byteString = " 123"
ParseString(byteString, NumberStyles.None)
ParseString(byteString, NumberStyles.Integer)
byteString = "3A"
ParseString(byteString, NumberStyles.AllowHexSpecifier)
byteString = "21"
ParseString(byteString, NumberStyles.Integer)
ParseString(byteString, NumberStyles.AllowHexSpecifier)
byteString = "-22"
ParseString(byteString, NumberStyles.Integer)
ParseString(byteString, NumberStyles.AllowParentheses)
byteString = "(45)"
ParseString(byteString, NumberStyles.AllowParentheses)
byteString = "000,000,056"
ParseString(byteString, NumberStyles.Integer)
ParseString(byteString, NumberStyles.Integer Or NumberStyles.AllowThousands)
End Sub
Private Sub ParseString(value As String, style As NumberStyles)
Dim number As SByte
If value Is Nothing Then Console.WriteLine("Cannot parse a null string...")
Try
number = SByte.Parse(value, style, NumberFormatInfo.CurrentInfo)
Console.WriteLine("SByte.Parse('{0}', {1}) = {2}", value, style, number)
Catch e As FormatException
Console.WriteLine("'{0}' and {1} throw a FormatException", value, style)
Catch e As OverflowException
Console.WriteLine("'{0}' is outside the range of a signed byte",
value)
End Try
End Sub
End Module
' The example displays the following information to the console:
' ' 123' and None throw a FormatException
' SByte.Parse(" 123", Integer)) = 123
' SByte.Parse("3A", AllowHexSpecifier)) = 58
' SByte.Parse("21", Integer)) = 21
' SByte.Parse("21", AllowHexSpecifier)) = 33
' SByte.Parse("-22", Integer)) = -22
' '-22' and AllowParentheses throw a FormatException
' SByte.Parse("(45)", AllowParentheses)) = -45
' '000,000,056' and Integer throw a FormatException
' SByte.Parse("000,000,056", Integer, AllowThousands)) = 56
備註
參數 style
會定義樣式專案 (,例如空白字元或正負號符號,) 參數中 s
允許的樣式專案,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。
根據 的值 style
, s
參數可能包含下列元素:
[ws][ $ ][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]
如果 style
包含 AllowHexSpecifier ,參數 s
可能包含下列元素:
[ws]hexdigits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 如果 style 包含 旗標, NumberStyles.AllowLeadingWhite 則空白字元可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾 s 。 |
$ | 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 屬性所 NumberFormatInfo.CurrencyPositivePattern 定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可以出現在 s 中。 |
簽署 | 選擇性符號。 如果 style 包含 旗標, NumberStyles.AllowLeadingSign 則符號可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,它可能會顯示 結尾 s 。 如果 style 包含 NumberStyles.AllowParentheses 旗標,可以使用 s 括弧來表示負值。 |
數字 | 從 0 到 9 的數位序列。 |
. | 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可以出現在 中 s 。 |
fractional_digits | 如果包含 旗標,則為 NumberStyles.AllowExponent 0-9 的一或多個出現次數,如果 style 不包含,則為一或多個數位 0。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s 。 |
E | 「e」 或 「E」 字元,表示值是以指數 (科學) 標記法表示。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent 。 |
exponential_digits | 從 0 到 9 的數位序列。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent 。 |
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元素。 |
NumberStyles.AllowTrailingWhite | 結尾處的 s ws元素。 |
NumberStyles.AllowLeadingSign | 數位之前的正負 號。 |
NumberStyles.AllowTrailingSign | 數位後面的正負 號。 |
NumberStyles.AllowParentheses | 在 數位 前後加上括弧,表示負值。 |
NumberStyles.AllowThousands | 群組分隔符號 () 元素。 雖然群組分隔符號可以出現在 中 s ,但前面必須只有一或多個 0 位數。 |
NumberStyles.AllowCurrencySymbol | 貨幣 ($) 專案。 |
NumberStyles.AllowHexSpecifier如果使用旗標, s
則必須是十六進位值。 有效的十六進位數位為 0-9、a-f 和 A-F。 唯一可以與其結合的其他旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 包含複合數位樣式, NumberStyles.HexNumber 其中包含空白字元旗標.)
注意
s
如果 參數是十六進位數的字串標記法,則不能在前面加上任何裝飾 (,例如 0x
或 &h
) ,將它區分為十六進位數。 這會導致剖析作業擲回例外狀況。
如果 s
代表十六進位數位, Parse(String, NumberStyles) 則方法會將位元組的高序位解譯為符號位。
參數 provider
是實作, IFormatProvider 其 GetFormat 方法會 NumberFormatInfo 傳回 物件,該物件提供有關 格式 s
的文化特性特定資訊。 有三種方式可以使用 provider
參數,將自訂格式資訊提供給剖析作業:
您可以傳遞提供格式資訊的實際 NumberFormatInfo 物件。 (其 實作 GetFormat 只會傳回本身。)
您可以傳遞 CultureInfo 物件,指定要使用其格式的文化特性。 其 NumberFormat 屬性提供格式化資訊。
您可以傳遞自訂 IFormatProvider 實作。 其 GetFormat 方法必須具現化並傳回 NumberFormatInfo 提供格式資訊的物件。
如果 為 provider
null
,則會 NumberFormatInfo 使用目前文化特性的物件。
適用於
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
重要
此 API 不符合 CLS 規範。
將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 8 位元帶正負號的整數。
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> sbyte
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> sbyte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As SByte
參數
- s
- ReadOnlySpan<Char>
範圍,其包含代表所要轉換數字的字元。 此範圍使用 style
指定的樣式來解譯。
- style
- NumberStyles
列舉值的位元組合,表示 s
中可以存在的樣式項目。 一般會指定的值是 Integer。
- provider
- IFormatProvider
物件,其提供關於 s
的特定文化特性格式資訊。 如果 provider
是 null
,則會使用執行緒目前的文化特性。
傳回
8 位元帶正負號的位元組值,等於 s
參數中所指定的數字。
實作
- 屬性
適用於
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將 UTF-8 字元的範圍剖析為值。
public static sbyte Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> sbyte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As SByte
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的 UTF-8 字元範圍。
- style
- NumberStyles
數位樣式的位元組合,可以存在於 中 utf8Text
。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
傳回
剖析 utf8Text
的結果。
實作
適用於
Parse(String, IFormatProvider)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將指定特定文化特性格式之數字的字串表示轉換成它的對等 8 位元帶正負號的整數。
public:
static System::SByte Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static System::SByte Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::SByte>::Parse;
[System.CLSCompliant(false)]
public static sbyte Parse (string s, IFormatProvider provider);
public static sbyte Parse (string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static sbyte Parse (string s, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> sbyte
static member Parse : string * IFormatProvider -> sbyte
Public Shared Function Parse (s As String, provider As IFormatProvider) As SByte
參數
- provider
- IFormatProvider
物件,其提供關於 s
的特定文化特性格式資訊。 如果 provider
是 null
,則會使用執行緒目前的文化特性。
傳回
8 位元帶正負號的整數,這個整數等於 s
中所指定的數字。
實作
- 屬性
例外狀況
s
為 null
。
s
的格式不正確。
s
代表小於 SByte.MinValue 或大於 SByte.MaxValue的數位。
範例
下列範例會定義自訂物件,該物件 NumberFormatInfo 會將波浪 (~) 定義為負號。 然後,它會使用這個自訂 NumberFormatInfo 物件以及 CultureInfo 代表不因文化特性而異的 物件來剖析一些數值字串。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
NumberFormatInfo nf = new NumberFormatInfo();
nf.NegativeSign = "~";
string[] values = { "-103", "+12", "~16", " 1", "~255" };
IFormatProvider[] providers = { nf, CultureInfo.InvariantCulture };
foreach (IFormatProvider provider in providers)
{
Console.WriteLine("Conversions using {0}:", ((object) provider).GetType().Name);
foreach (string value in values)
{
try {
Console.WriteLine(" Converted '{0}' to {1}.",
value, SByte.Parse(value, provider));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the SByte type.", value);
}
}
}
}
}
// The example displays the following output:
// Conversions using NumberFormatInfo:
// Unable to parse '-103'.
// Converted '+12' to 12.
// Converted '~16' to -16.
// Converted ' 1' to 1.
// '~255' is out of range of the SByte type.
// Conversions using CultureInfo:
// Converted '-103' to -103.
// Converted '+12' to 12.
// Unable to parse '~16'.
// Converted ' 1' to 1.
// Unable to parse '~255'.
open System
open System.Globalization
let nf = NumberFormatInfo()
nf.NegativeSign <- "~"
let values = [| "-103"; "+12"; "~16"; " 1"; "~255" |]
let providers: IFormatProvider[] = [| nf; CultureInfo.InvariantCulture |]
for provider in providers do
printfn $"Conversions using {(box provider).GetType().Name}:"
for value in values do
try
printfn $" Converted '{value}' to {SByte.Parse(value, provider)}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the SByte type."
// The example displays the following output:
// Conversions using NumberFormatInfo:
// Unable to parse '-103'.
// Converted '+12' to 12.
// Converted '~16' to -16.
// Converted ' 1' to 1.
// '~255' is out of range of the SByte type.
// Conversions using CultureInfo:
// Converted '-103' to -103.
// Converted '+12' to 12.
// Unable to parse '~16'.
// Converted ' 1' to 1.
// Unable to parse '~255'.
Imports System.Globalization
Module Example
Public Sub Main()
Dim nf As New NumberFormatInfo()
nf.NegativeSign = "~"
Dim values() As String = { "-103", "+12", "~16", " 1", "~255" }
Dim providers() As IFormatProvider = { nf, CultureInfo.InvariantCulture }
For Each provider As IFormatProvider In providers
Console.WriteLine("Conversions using {0}:", CObj(provider).GetType().Name)
For Each value As String In values
Try
Console.WriteLine(" Converted '{0}' to {1}.", _
value, SByte.Parse(value, provider))
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of range of the SByte type.", value)
End Try
Next
Next
End Sub
End Module
' The example displays '
' Conversions using NumberFormatInfo:
' Unable to parse '-103'.
' Converted '+12' to 12.
' Converted '~16' to -16.
' Converted ' 1' to 1.
' '~255' is out of range of the SByte type.
' Conversions using CultureInfo:
' Converted '-103' to -103.
' Converted '+12' to 12.
' Unable to parse '~16'.
' Converted ' 1' to 1.
' Unable to parse '~255'.
備註
參數 s
包含一些格式:
[ws][sign]digits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 |
簽署 | 選擇性符號。 |
數字 | 範圍從 0 到 9 的數位序列。 |
參數 s
會使用 Integer 樣式來解譯。 除了位元組值的十進位數之外,只允許具有前置符號的前置和尾端空格。 若要使用可以存在於 中的 s
特定文化特性格式資訊明確定義樣式專案,請使用 Parse(String, NumberStyles, IFormatProvider) 方法。
參數 provider
是實作, IFormatProvider 其 GetFormat 方法會 NumberFormatInfo 傳回 物件,該物件提供有關 格式 s
的文化特性特定資訊。 有三種方式可以使用 provider
參數,將自訂格式資訊提供給剖析作業:
您可以傳遞提供格式資訊的實際 NumberFormatInfo 物件。 (其 實作 GetFormat 只會傳回本身。)
您可以傳遞 CultureInfo 物件,指定要使用其格式的文化特性。 其 NumberFormat 屬性提供格式化資訊。
您可以傳遞自訂 IFormatProvider 實作。 其 GetFormat 方法必須具現化並傳回 NumberFormatInfo 提供格式資訊的物件。
如果 為 provider
null
,則會 NumberFormatInfo 使用目前文化特性的物件。
另請參閱
適用於
Parse(String)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將數字的字串表示轉換成它的對等 8 位元帶正負號的整數。
public:
static System::SByte Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static sbyte Parse (string s);
public static sbyte Parse (string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> sbyte
static member Parse : string -> sbyte
Public Shared Function Parse (s As String) As SByte
參數
傳回
8 位元帶正負號的整數,這個整數等於 s
參數中所包含的數字。
- 屬性
例外狀況
s
為 null
。
s
不是由選擇性正負號後面接著一連串數字 (零到九) 所組成。
s
代表小於 SByte.MinValue 或大於 SByte.MaxValue的數位。
範例
下列範例示範如何使用 方法,將字串值轉換成帶正負號的位元組值 Parse 。 產生的帶正負號位元組值接著會顯示至主控台。
// Define an array of numeric strings.
string[] values = { "-16", " -3", "+ 12", " +12 ", " 12 ",
"+120", "(103)", "192", "-160" };
// Parse each string and display the result.
foreach (string value in values)
{
try {
Console.WriteLine("Converted '{0}' to the SByte value {1}.",
value, SByte.Parse(value));
}
catch (FormatException) {
Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",
value);
}
catch (OverflowException) {
Console.WriteLine("'{0}' is out of range of the SByte type.",
value);
}
}
// The example displays the following output:
// Converted '-16' to the SByte value -16.
// Converted ' -3' to the SByte value -3.
// '+ 12' cannot be parsed successfully by SByte type.
// Converted ' +12 ' to the SByte value 12.
// Converted ' 12 ' to the SByte value 12.
// Converted '+120' to the SByte value 120.
// '(103)' cannot be parsed successfully by SByte type.
// '192' is out of range of the SByte type.
// '-160' is out of range of the SByte type.
open System
// Define an array of numeric strings.
let values =
[| "-16"; " -3"; "+ 12"; " +12 "; " 12 "
"+120"; "(103)"; "192"; "-160" |]
// Parse each string and display the result.
for value in values do
try
printfn $"Converted '{value}' to the SByte value {SByte.Parse value}."
with
| :? FormatException ->
printfn $"'{value}' cannot be parsed successfully by SByte type."
| :? OverflowException ->
printfn $"'{value}' is out of range of the SByte type."
// The example displays the following output:
// Converted '-16' to the SByte value -16.
// Converted ' -3' to the SByte value -3.
// '+ 12' cannot be parsed successfully by SByte type.
// Converted ' +12 ' to the SByte value 12.
// Converted ' 12 ' to the SByte value 12.
// Converted '+120' to the SByte value 120.
// '(103)' cannot be parsed successfully by SByte type.
// '192' is out of range of the SByte type.
// '-160' is out of range of the SByte type.
' Define an array of numeric strings.
Dim values() As String = { "-16", " -3", "+ 12", " +12 ", " 12 ", _
"+120", "(103)", "192", "-160" }
' Parse each string and display the result.
For Each value As String In values
Try
Console.WriteLine("Converted '{0}' to the SByte value {1}.", _
value, SByte.Parse(value))
Catch e As FormatException
Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.", _
value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the SByte type.", _
value)
End Try
Next
' The example displays the following output:
' Converted '-16' to the SByte value -16.
' Converted ' -3' to the SByte value -3.
' '+ 12' cannot be parsed successfully by SByte type.
' Converted ' +12 ' to the SByte value 12.
' Converted ' 12 ' to the SByte value 12.
' Converted '+120' to the SByte value 120.
' '(103)' cannot be parsed successfully by SByte type.
' '192' is out of range of the SByte type.
' '-160' is out of range of the SByte type.
備註
參數 s
包含一些格式:
[ws][sign]digits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 |
簽署 | 選擇性符號。 |
數字 | 範圍從 0 到 9 的數位序列。 |
參數 s
會使用 NumberStyles.Integer 樣式來解譯。 除了位元組值的十進位數之外,只允許具有前置正負號的前置和尾端空格。 若要明確定義可以存在於 中的 s
樣式專案,請使用 Parse(String, NumberStyles) 或 Parse(String, NumberStyles, IFormatProvider) 方法。
參數 s
是使用針對目前系統文化特性初始化的 中 NumberFormatInfo 格式化資訊來剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要使用其他文化特性的格式資訊剖析字串,請使用 Parse(String, NumberStyles, IFormatProvider) 方法。
另請參閱
適用於
Parse(ReadOnlySpan<Char>, IFormatProvider)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將字元範圍剖析為值。
public:
static System::SByte Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::SByte>::Parse;
public static sbyte Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> sbyte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As SByte
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
傳回
剖析 s
的結果。
實作
適用於
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將 UTF-8 字元的範圍剖析為值。
public:
static System::SByte Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::SByte>::Parse;
public static sbyte Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> sbyte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As SByte
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的 UTF-8 字元範圍。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
傳回
剖析 utf8Text
的結果。
實作
適用於
Parse(String, NumberStyles)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
將指定樣式之數字的字串表示轉換成它的對等 8 位元帶正負號的整數。
public:
static System::SByte Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style);
public static sbyte Parse (string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> sbyte
static member Parse : string * System.Globalization.NumberStyles -> sbyte
Public Shared Function Parse (s As String, style As NumberStyles) As SByte
參數
- s
- String
字串,其包含要轉換的數字。 這個字串使用 style
指定的樣式來解譯。
- style
- NumberStyles
列舉值的位元組合,表示 s
中可以存在的樣式項目。 一般會指定的值是 Integer。
傳回
8 位元帶正負號的整數,這個整數等於 s
中所指定的數字。
- 屬性
例外狀況
s
為 null
。
s
的格式與 style
不相容。
範例
下列範例會使用 Parse(String, NumberStyles) 方法剖析值的字串表示 SByte 。 此範例的目前文化特性為 en-US。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
NumberStyles style;
sbyte number;
// Parse value with no styles allowed.
string[] values1 = { " 121 ", "121", "-121" };
style = NumberStyles.None;
Console.WriteLine("Styles: {0}", style.ToString());
foreach (string value in values1)
{
try {
number = SByte.Parse(value, style);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
}
Console.WriteLine();
// Parse value with trailing sign.
style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
string[] values2 = { " 103+", " 103 +", "+103", "(103)", " +103 " };
Console.WriteLine("Styles: {0}", style.ToString());
foreach (string value in values2)
{
try {
number = SByte.Parse(value, style);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the SByte type.", value);
}
}
Console.WriteLine();
}
}
// The example displays the following output:
// Styles: None
// Unable to parse ' 121 '.
// Converted '121' to 121.
// Unable to parse '-121'.
//
// Styles: Integer, AllowTrailingSign
// Converted ' 103+' to 103.
// Converted ' 103 +' to 103.
// Converted '+103' to 103.
// Unable to parse '(103)'.
// Converted ' +103 ' to 103.
open System
open System.Globalization
// Parse value with no styles allowed.
let values1 = [| " 121 "; "121"; "-121" |]
let style = NumberStyles.None
printfn $"Styles: {style}"
for value in values1 do
try
let number = SByte.Parse(value, style)
printfn $" Converted '{value}' to {number}."
with :? FormatException ->
printfn $" Unable to parse '{value}'."
printfn ""
// Parse value with trailing sign.
let style2 = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
let values2 = [| " 103+"; " 103 +"; "+103"; "(103)"; " +103 " |]
printfn $"Styles: {style2}"
for value in values2 do
try
let number = SByte.Parse(value, style2)
printfn $" Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the SByte type."
printfn ""
// The example displays the following output:
// Styles: None
// Unable to parse ' 121 '.
// Converted '121' to 121.
// Unable to parse '-121'.
//
// Styles: Integer, AllowTrailingSign
// Converted ' 103+' to 103.
// Converted ' 103 +' to 103.
// Converted '+103' to 103.
// Unable to parse '(103)'.
// Converted ' +103 ' to 103.
Imports System.Globalization
Module Example
Public Sub Main()
Dim style As NumberStyles
Dim number As SByte
' Parse value with no styles allowed.
Dim values1() As String = { " 121 ", "121", "-121" }
style = NumberStyles.None
Console.WriteLine("Styles: {0}", style.ToString())
For Each value As String In values1
Try
number = SByte.Parse(value, style)
Console.WriteLine(" Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
End Try
Next
Console.WriteLine()
' Parse value with trailing sign.
style = NumberStyles.Integer Or NumberStyles.AllowTrailingSign
Dim values2() As String = { " 103+", " 103 +", "+103", "(103)", " +103 " }
Console.WriteLine("Styles: {0}", style.ToString())
For Each value As String In values2
Try
number = SByte.Parse(value, style)
Console.WriteLine(" Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of range of the SByte type.", value)
End Try
Next
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' Styles: None
' Unable to parse ' 121 '.
' Converted '121' to 121.
' Unable to parse '-121'.
'
' Styles: Integer, AllowTrailingSign
' Converted ' 103+' to 103.
' Converted ' 103 +' to 103.
' Converted '+103' to 103.
' Unable to parse '(103)'.
' Converted ' +103 ' to 103.
備註
參數 style
會定義樣式專案 (,例如空白字元或正負號符號,) 參數中 s
允許的樣式專案,讓剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。
根據 的值 style
, s
參數可能包含下列元素:
[ws][ $ ][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]
如果 style
包含 NumberStyles.AllowHexSpecifier ,參數 s
可能包含下列元素:
[ws]hexdigits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空白字元。 如果 包含 style 旗標, NumberStyles.AllowLeadingWhite 則空白字元可以出現在 開頭 s ,如果樣式包含 NumberStyles.AllowTrailingWhite 旗標,則它可能會出現在 結尾 s 。 |
$ | 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 屬性所 NumberFormatInfo.CurrencyPositivePattern 定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可以出現在 s 中。 |
簽署 | 選擇性符號。 如果 style 包含 旗標, NumberStyles.AllowLeadingSign 則符號可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 結尾 s 。 如果 style 包含 NumberStyles.AllowParentheses 旗標,可以使用 s 括弧來表示負值。 |
數字 | 從 0 到 9 的數位序列。 |
. | 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可以出現在 中 s 。 |
fractional_digits | 如果包含 旗標,則為 NumberStyles.AllowExponent 0-9 的一或多個出現次數,如果 style 不包含,則為一或多個數位 0。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s 。 |
E | 「e」 或 「E」 字元,表示值是以指數 (科學) 標記法表示。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent 。 |
exponential_digits | 數位 0-9 的一或多個出現次數。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent 。 |
hexdigits | 從 0 到 f 或 0 到 F 的十六進位數位序列。 |
注意
不論引數的值 style
為何,剖析作業都會忽略 中 s
任何終止的 NUL (U+0000) 字元。
只有十進位數的字串, (對應至 NumberStyles.None 樣式) 一律會成功剖析。 大部分剩餘 NumberStyles 的成員控制項元素可能存在,但不需要出現在輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s
可能存在的專案。
非複合 NumberStyles 值 | 除了數位之外,也允許的專案 |
---|---|
NumberStyles.None | 僅限十進位數。 |
NumberStyles.AllowDecimalPoint | 小數點 (。) 和 fractional_digits 元素。 不過,如果 style 不包含 NumberStyles.AllowExponent 旗標, fractional_digits 必須只包含一或多個 0 位數,否則 OverflowException 會擲回 。 |
NumberStyles.AllowExponent | 「e」 或 「E」 字元,表示指數標記法,以及 exponential_digits。 |
NumberStyles.AllowLeadingWhite | 開頭的 s ws元素。 |
NumberStyles.AllowTrailingWhite | 結尾處的 s ws元素。 |
NumberStyles.AllowLeadingSign | 數位之前的正負 號。 |
NumberStyles.AllowTrailingSign | 數位後面的正負 號。 |
NumberStyles.AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
NumberStyles.AllowThousands | 群組分隔符號 (,) 專案。 雖然群組分隔符號可以出現在 中 s ,但前面必須只有一或多個 0 位數。 |
NumberStyles.AllowCurrencySymbol | 貨幣 ($) 專案。 |
NumberStyles.AllowHexSpecifier如果使用旗標, s
則必須是十六進位值。 有效的十六進位數位為 0-9、a-f 和 A-F。 不支援 「0x」 之類的前置詞,並導致剖析作業失敗。 唯一可以合併于 中的 style
其他旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 包含複合數位樣式, NumberStyles.HexNumber 其中包含空白字元旗標.)
注意
s
如果 參數是十六進位數的字串標記法,則不能在前面加上任何裝飾 (,例如 0x
或 &h
) ,將它區分為十六進位數。 這會導致剖析作業擲回例外狀況。
如果 s
代表十六進位數位, Parse(String, NumberStyles) 則方法會將位元組的高序位解譯為符號位。
參數 s
是使用物件中 NumberFormatInfo 針對目前系統文化特性初始化的格式資訊進行剖析。 若要使用某些其他文化特性的格式資訊,請呼叫 Parse(String, NumberStyles, IFormatProvider) 多載。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應