UInt16.Parse Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengonversi representasi string angka ke bilangan bulat tidak bertanda 16-bit yang setara.
Overload
Parse(String, NumberStyles, IFormatProvider) |
Mengonversi representasi string angka dalam gaya tertentu dan format khusus budaya ke bilangan bulat tidak bertanda 16-bit yang setara. |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Mengonversi representasi rentang angka dalam gaya tertentu dan format khusus budaya ke bilangan bulat tidak bertanda 16-bit yang setara. |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
Menguraikan rentang karakter UTF-8 ke dalam nilai. |
Parse(String, IFormatProvider) |
Mengonversi representasi string dari angka dalam format khusus budaya tertentu ke bilangan bulat tidak bertanda 16-bit yang setara. |
Parse(String, NumberStyles) |
Mengonversi representasi string angka dalam gaya tertentu ke bilangan bulat tidak bertanda 16-bit yang setara. Metode ini tidak sesuai dengan CLS. Alternatif yang mematuhi CLS adalah Parse(String, NumberStyles). |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
Mengurai rentang karakter ke dalam nilai. |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
Menguraikan rentang karakter UTF-8 ke dalam nilai. |
Parse(String) |
Mengonversi representasi string angka ke bilangan bulat tidak bertanda 16-bit yang setara. |
Parse(String, NumberStyles, IFormatProvider)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Mengonversi representasi string angka dalam gaya tertentu dan format khusus budaya ke bilangan bulat tidak bertanda 16-bit yang setara.
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::UInt16>::Parse;
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint16
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As UShort
Parameter
- s
- String
String yang mewakili angka yang akan dikonversi. String ditafsirkan dengan menggunakan gaya yang ditentukan oleh style
parameter .
- style
- NumberStyles
Kombinasi bitwise nilai enumerasi yang menunjukkan elemen gaya yang dapat ada di s
. Nilai umum yang akan ditentukan adalah Integer.
- provider
- IFormatProvider
Objek yang memasok informasi pemformatan khusus budaya tentang s
.
Mengembalikan
Bilangan bulat 16-bit yang tidak ditandatangani setara dengan angka yang ditentukan dalam s
.
Penerapan
- Atribut
Pengecualian
s
adalah null
.
style
bukan nilai NumberStyles .
-atau-
style
bukan kombinasi nilai AllowHexSpecifier dan HexNumber .
s
tidak dalam format yang sesuai dengan style
.
s
mewakili angka yang kurang dari UInt16.MinValue atau lebih besar dari UInt16.MaxValue.
-atau-
s
termasuk bukan nol, digit pecahan.
Contoh
Contoh berikut menggunakan Parse(String, NumberStyles, IFormatProvider) metode untuk mengonversi berbagai representasi string angka menjadi nilai bilangan bulat yang tidak ditandatangani 16-bit.
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 = { "1702", "+1702.0", "+1702,0", "-1032.00",
"-1032,00", "1045.1", "1045,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,
UInt16.Parse(value, style, ci));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the UInt16 type.",
value);
}
}
}
}
}
}
// The example displays the following output:
// Parsing strings using the English (United States) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Converted '+1702.0' to 1702.
// Unable to parse '+1702,0'.
// '-1032.00' is out of range of the UInt16 type.
// Unable to parse '-1032,00'.
// '1045.1' is out of range of the UInt16 type.
// Unable to parse '1045,1'.
// Parsing strings using the French (France) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Converted '+1702,0' to 1702.
// Unable to parse '-1032.00'.
// '-1032,00' is out of range of the UInt16 type.
// Unable to parse '1045.1'.
// '1045,1' is out of range of the UInt16 type.
open System
open System.Globalization
let cultureNames = [| "en-US"; "fr-FR" |]
let styles =
[| NumberStyles.Integer; NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint |]
let values =
[| "1702"; "+1702.0"; "+1702,0"; "-1032.00"; "-1032,00"; "1045.1"; "1045,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 {UInt16.Parse(value, style, ci)}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the UInt16 type."
// The example displays the following output:
// Parsing strings using the English (United States) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Converted '+1702.0' to 1702.
// Unable to parse '+1702,0'.
// '-1032.00' is out of range of the UInt16 type.
// Unable to parse '-1032,00'.
// '1045.1' is out of range of the UInt16 type.
// Unable to parse '1045,1'.
// Parsing strings using the French (France) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Converted '+1702,0' to 1702.
// Unable to parse '-1032.00'.
// '-1032,00' is out of range of the UInt16 type.
// Unable to parse '1045.1'.
// '1045,1' is out of range of the UInt16 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 = { "1702", "+1702.0", "+1702,0", "-1032.00", _
"-1032,00", "1045.1", "1045,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, _
UInt16.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 UInt16 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 '1702' to 1702.
' Unable to parse '+1702.0'.
' Unable to parse '+1702,0'.
' Unable to parse '-1032.00'.
' Unable to parse '-1032,00'.
' Unable to parse '1045.1'.
' Unable to parse '1045,1'.
' Style: Integer, AllowDecimalPoint
' Converted '1702' to 1702.
' Converted '+1702.0' to 1702.
' Unable to parse '+1702,0'.
' '-1032.00' is out of range of the UInt16 type.
' Unable to parse '-1032,00'.
' '1045.1' is out of range of the UInt16 type.
' Unable to parse '1045,1'.
' Parsing strings using the French (France) culture
' Style: Integer
' Converted '1702' to 1702.
' Unable to parse '+1702.0'.
' Unable to parse '+1702,0'.
' Unable to parse '-1032.00'.
' Unable to parse '-1032,00'.
' Unable to parse '1045.1'.
' Unable to parse '1045,1'.
' Style: Integer, AllowDecimalPoint
' Converted '1702' to 1702.
' Unable to parse '+1702.0'.
' Converted '+1702,0' to 1702.
' Unable to parse '-1032.00'.
' '-1032,00' is out of range of the UInt16 type.
' Unable to parse '1045.1'.
' '1045,1' is out of range of the UInt16 type.
Keterangan
Parameter style
menentukan elemen gaya (seperti spasi kosong atau simbol tanda positif atau negatif) yang diizinkan dalam s
parameter agar operasi penguraian berhasil. Ini harus berupa kombinasi bendera bit dari NumberStyles enumerasi.
Bergantung pada nilai style
, s
parameter dapat mencakup elemen berikut:
[ws] [$][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]
Elemen dalam tanda kurung siku ([ dan ]) adalah opsional. Jika style
menyertakan NumberStyles.AllowHexSpecifier, s
parameter dapat menyertakan elemen berikut:
[ws] hexdigits[ws]
Tabel berikut ini menjelaskan setiap elemen.
Elemen | Deskripsi |
---|---|
Ws | Spasi kosong opsional. Spasi kosong dapat muncul di awal s jika style menyertakan NumberStyles.AllowLeadingWhite bendera, dan dapat muncul di akhir s jika style menyertakan NumberStyles.AllowTrailingWhite bendera. |
$ | Simbol mata uang khusus budaya. Posisinya dalam string didefinisikan oleh CurrencyPositivePattern properti NumberFormatInfo objek yang dikembalikan oleh GetFormat metode provider parameter . Simbol mata uang dapat muncul jika s style menyertakan NumberStyles.AllowCurrencySymbol bendera . |
sign | Tanda opsional. (Metode ini melemparkan OverflowException jika s menyertakan tanda negatif dan mewakili angka bukan nol.) Tanda dapat muncul di awal s jika style menyertakan NumberStyles.AllowLeadingSign bendera, dan tanda tersebut dapat muncul di akhir s jika style menyertakan NumberStyles.AllowTrailingSign bendera. Tanda kurung dapat digunakan s untuk menunjukkan nilai negatif jika style menyertakan NumberStyles.AllowParentheses bendera . |
Digit | Urutan digit dari 0 hingga 9. |
. | Simbol titik desimal khusus budaya. Simbol titik desimal budaya saat ini dapat muncul jika s style menyertakan NumberStyles.AllowDecimalPoint bendera . |
fractional_digits | Satu atau beberapa kemunculan digit 0-9 jika style menyertakan NumberStyles.AllowExponent bendera, atau satu atau beberapa kemunculan digit 0 jika tidak. Digit pecahan dapat muncul s hanya jika style menyertakan NumberStyles.AllowDecimalPoint bendera. |
E | Karakter "e" atau "E", yang menunjukkan bahwa nilai diwakili dalam notasi eksponensial (ilmiah). Parameter s dapat mewakili angka dalam notasi eksponensial jika style menyertakan NumberStyles.AllowExponent bendera . |
exponential_digits | Urutan digit dari 0 hingga 9. Parameter s dapat mewakili angka dalam notasi eksponensial jika style menyertakan NumberStyles.AllowExponent bendera . |
hexdigits | Urutan digit heksadesimal dari 0 hingga f, atau 0 hingga F. |
Catatan
Setiap karakter NUL yang mengakhiri (U+0000) di s
diabaikan oleh operasi penguraian, terlepas dari style
nilai argumen.
String dengan digit desimal saja (yang sesuai dengan NumberStyles.None gaya) selalu berhasil diurai. Sebagian besar elemen kontrol anggota yang tersisa NumberStyles yang mungkin ada, tetapi tidak diharuskan ada, dalam string input ini. Tabel berikut menunjukkan bagaimana anggota individu NumberStyles memengaruhi elemen yang mungkin ada di s
.
Nilai non-komposit NumberStyles |
Elemen yang diizinkan selain s digit |
---|---|
NumberStyles.None | Digit desimal saja. |
NumberStyles.AllowDecimalPoint | Titik desimal (.) dan elemen fractional_digits . Namun, jika gaya tidak menyertakan NumberStyles.AllowExponent bendera, fractional_digits harus terdiri dari hanya satu atau lebih 0 digit; jika tidak, akan OverflowException dilemparkan. |
NumberStyles.AllowExponent | Karakter "e" atau "E", yang menunjukkan notasi eksponensial, bersama dengan exponential_digits. |
NumberStyles.AllowLeadingWhite | Elemen ws di awal s . |
NumberStyles.AllowTrailingWhite | Elemen ws di akhir s . |
NumberStyles.AllowLeadingSign | Tanda sebelum digit. |
NumberStyles.AllowTrailingSign | Tanda setelah digit. |
NumberStyles.AllowParentheses | Tanda kurung sebelum dan sesudah digit untuk menunjukkan nilai negatif. |
NumberStyles.AllowThousands | Elemen pemisah grup (,). |
NumberStyles.AllowCurrencySymbol | Elemen mata uang ($). |
NumberStyles.AllowHexSpecifier Jika bendera digunakan, s
harus berupa nilai heksadesimal. Digit heksadesimal yang valid adalah 0 hingga 9, hingga f, dan A hingga F. Awalan, seperti "0x", tidak didukung dan menyebabkan operasi penguraian gagal. Satu-satunya bendera lain yang dapat digabungkan dengan NumberStyles.AllowHexSpecifier adalah NumberStyles.AllowLeadingWhite dan NumberStyles.AllowTrailingWhite. (Enumerasi NumberStyles mencakup gaya angka komposit, NumberStyles.HexNumber, yang mencakup kedua bendera spasi putih.)
Catatan
s
Jika parameter adalah representasi string dari angka heksadesimal, parameter tersebut tidak dapat didahului oleh dekorasi apa pun (seperti 0x
atau &h
) yang membedakannya sebagai angka heksadesimal. Hal ini menyebabkan operasi penguraian melempar pengecualian.
Parameter provider
adalah IFormatProvider implementasi yang metodenya GetFormat mengembalikan NumberFormatInfo objek yang menyediakan informasi khusus budaya tentang format s
. Ada tiga cara untuk menggunakan provider
parameter untuk menyediakan informasi pemformatan kustom ke operasi penguraian:
Anda dapat meneruskan objek aktual NumberFormatInfo yang menyediakan informasi pemformatan. (Implementasinya GetFormat hanya mengembalikan dirinya sendiri.)
Anda dapat meneruskan CultureInfo objek yang menentukan budaya yang pemformatannya akan digunakan. Propertinya NumberFormat menyediakan informasi pemformatan.
Anda dapat melewati implementasi kustom IFormatProvider . Metodenya GetFormat harus membuat instans dan mengembalikan NumberFormatInfo objek yang menyediakan informasi pemformatan.
Jika provider
adalah null
, NumberFormatInfo objek untuk budaya saat ini digunakan.
Lihat juga
Berlaku untuk
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Penting
API ini bukan kompatibel CLS.
Mengonversi representasi rentang angka dalam gaya tertentu dan format khusus budaya ke bilangan bulat tidak bertanda 16-bit yang setara.
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint16
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UShort
Parameter
- s
- ReadOnlySpan<Char>
Rentang yang berisi karakter yang mewakili angka yang akan dikonversi. Rentang ditafsirkan dengan menggunakan gaya yang ditentukan oleh style
parameter .
- style
- NumberStyles
Kombinasi bitwise nilai enumerasi yang menunjukkan elemen gaya yang dapat ada di s
. Nilai umum yang akan ditentukan adalah Integer.
- provider
- IFormatProvider
Objek yang memasok informasi pemformatan khusus budaya tentang s
.
Mengembalikan
Bilangan bulat 16-bit yang tidak ditandatangani setara dengan angka yang ditentukan dalam s
.
Penerapan
- Atribut
Berlaku untuk
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Menguraikan rentang karakter UTF-8 ke dalam nilai.
public static ushort Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UShort
Parameter
- utf8Text
- ReadOnlySpan<Byte>
Rentang karakter UTF-8 untuk diurai.
- style
- NumberStyles
Kombinasi bitwise gaya angka yang dapat ada di utf8Text
.
- provider
- IFormatProvider
Objek yang menyediakan informasi pemformatan khusus budaya tentang utf8Text
.
Mengembalikan
Hasil penguraian utf8Text
.
Penerapan
Berlaku untuk
Parse(String, IFormatProvider)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Mengonversi representasi string dari angka dalam format khusus budaya tertentu ke bilangan bulat tidak bertanda 16-bit yang setara.
public:
static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::UInt16>::Parse;
[System.CLSCompliant(false)]
public static ushort Parse (string s, IFormatProvider provider);
public static ushort Parse (string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ushort Parse (string s, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint16
static member Parse : string * IFormatProvider -> uint16
Public Shared Function Parse (s As String, provider As IFormatProvider) As UShort
Parameter
- s
- String
String yang mewakili angka yang akan dikonversi.
- provider
- IFormatProvider
Objek yang memasok informasi pemformatan khusus budaya tentang s
.
Mengembalikan
Bilangan bulat 16-bit yang tidak ditandatangani setara dengan angka yang ditentukan dalam s
.
Penerapan
- Atribut
Pengecualian
s
adalah null
.
s
tidak dalam format yang benar.
s
mewakili angka yang kurang dari UInt16.MinValue atau lebih besar dari UInt16.MaxValue.
Contoh
Contoh berikut membuat instans budaya kustom yang menggunakan dua tanda plus (++) sebagai tanda positifnya. Kemudian memanggil Parse(String, IFormatProvider) metode untuk mengurai array string dengan menggunakan CultureInfo objek yang mewakili budaya kustom ini dan budaya invarian.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Define a custom culture that uses "++" as a positive sign.
CultureInfo ci = new CultureInfo("");
ci.NumberFormat.PositiveSign = "++";
// Create an array of cultures.
CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
// Create an array of strings to parse.
string[] values = { "++1403", "-0", "+0", "+16034",
Int16.MinValue.ToString(), "14.0", "18012" };
// Parse the strings using each culture.
foreach (CultureInfo culture in cultures)
{
Console.WriteLine("Parsing with the '{0}' culture.", culture.Name);
foreach (string value in values)
{
try {
ushort number = UInt16.Parse(value, culture);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" The format of '{0}' is invalid.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is outside the range of a UInt16 value.", value);
}
}
}
}
}
// The example displays the following output:
// Parsing with the culture.
// Converted '++1403' to 1403.
// Converted '-0' to 0.
// The format of '+0' is invalid.
// The format of '+16034' is invalid.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
// Parsing with the '' culture.
// The format of '++1403' is invalid.
// Converted '-0' to 0.
// Converted '+0' to 0.
// Converted '+16034' to 16034.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
open System
open System.Globalization
// Define a custom culture that uses "++" as a positive sign.
let ci = CultureInfo ""
ci.NumberFormat.PositiveSign <- "++"
// Create an array of cultures.
let cultures = [| ci; CultureInfo.InvariantCulture |]
// Create an array of strings to parse.
let values =
[| "++1403"; "-0"; "+0"; "+16034"
string Int16.MinValue; "14.0"; "18012" |]
// Parse the strings using each culture.
for culture in cultures do
printfn $"Parsing with the '{culture.Name}' culture."
for value in values do
try
let number = UInt16.Parse(value, culture)
printfn $" Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $" The format of '{value}' is invalid."
| :? OverflowException ->
printfn $" '{value}' is outside the range of a UInt16 value."
// The example displays the following output:
// Parsing with the culture.
// Converted '++1403' to 1403.
// Converted '-0' to 0.
// The format of '+0' is invalid.
// The format of '+16034' is invalid.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
// Parsing with the '' culture.
// The format of '++1403' is invalid.
// Converted '-0' to 0.
// Converted '+0' to 0.
// Converted '+16034' to 16034.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
Imports System.Globalization
Module Example
Public Sub Main()
' Define a custom culture that uses "++" as a positive sign.
Dim ci As CultureInfo = New CultureInfo("")
ci.NumberFormat.PositiveSign = "++"
' Create an array of cultures.
Dim cultures() As CultureInfo = { ci, CultureInfo.InvariantCulture }
' Create an array of strings to parse.
Dim values() As String = { "++1403", "-0", "+0", "+16034", _
Int16.MinValue.ToString(), "14.0", "18012" }
' Parse the strings using each culture.
For Each culture As CultureInfo In cultures
Console.WriteLine("Parsing with the '{0}' culture.", culture.Name)
For Each value As String In values
Try
Dim number As UShort = UInt16.Parse(value, culture)
Console.WriteLine(" Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine(" The format of '{0}' is invalid.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is outside the range of a UInt16 value.", value)
End Try
Next
Next
End Sub
End Module
' The example displays the following output:
' Parsing with the culture.
' Converted '++1403' to 1403.
' Converted '-0' to 0.
' The format of '+0' is invalid.
' The format of '+16034' is invalid.
' '-32768' is outside the range of a UInt16 value.
' The format of '14.0' is invalid.
' Converted '18012' to 18012.
' Parsing with the '' culture.
' The format of '++1403' is invalid.
' Converted '-0' to 0.
' Converted '+0' to 0.
' Converted '+16034' to 16034.
' '-32768' is outside the range of a UInt16 value.
' The format of '14.0' is invalid.
' Converted '18012' to 18012.
Keterangan
Parameter s
berisi sejumlah formulir:
[ws] [tanda]digits[ws]
Item dalam tanda kurung siku ([ dan ]) bersifat opsional. Tabel berikut ini menjelaskan setiap elemen.
Elemen | Deskripsi |
---|---|
Ws | Spasi kosong opsional. |
sign | Tanda opsional, atau tanda negatif jika s mewakili nilai nol. |
Digit | Urutan digit mulai dari 0 hingga 9. |
Parameter s ditafsirkan menggunakan NumberStyles.Integer gaya . Selain digit desimal nilai byte, hanya spasi di depan dan di belakang bersama dengan tanda di depan yang diizinkan. (Jika tanda negatif ada, s
harus mewakili nilai nol atau metode melempar OverflowException.) Untuk secara eksplisit menentukan elemen gaya bersama dengan informasi pemformatan khusus budaya yang dapat ada di s
, gunakan Parse(String, NumberStyles, IFormatProvider) metode .
Parameter provider
adalah IFormatProvider implementasi yang metodenya GetFormat mengembalikan NumberFormatInfo objek yang menyediakan informasi khusus budaya tentang format s
. Ada tiga cara untuk menggunakan provider
parameter untuk menyediakan informasi pemformatan kustom ke operasi penguraian:
Anda dapat meneruskan objek aktual NumberFormatInfo yang menyediakan informasi pemformatan. (Implementasinya GetFormat hanya mengembalikan dirinya sendiri.)
Anda dapat meneruskan CultureInfo objek yang menentukan budaya yang pemformatannya akan digunakan. Propertinya NumberFormat menyediakan informasi pemformatan.
Anda dapat melewati implementasi kustom IFormatProvider . Metodenya GetFormat harus membuat instans dan mengembalikan NumberFormatInfo objek yang menyediakan informasi pemformatan.
Jika provider
adalah null
, NumberFormatInfo untuk budaya saat ini digunakan.
Lihat juga
Berlaku untuk
Parse(String, NumberStyles)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Penting
API ini bukan kompatibel CLS.
Mengonversi representasi string angka dalam gaya tertentu ke bilangan bulat tidak bertanda 16-bit yang setara.
Metode ini tidak sesuai dengan CLS. Alternatif yang mematuhi CLS adalah Parse(String, NumberStyles).
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style);
public static ushort Parse (string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint16
static member Parse : string * System.Globalization.NumberStyles -> uint16
Public Shared Function Parse (s As String, style As NumberStyles) As UShort
Parameter
- s
- String
String yang mewakili angka yang akan dikonversi. String ditafsirkan dengan menggunakan gaya yang ditentukan oleh style
parameter .
- style
- NumberStyles
Kombinasi bitwise dari nilai enumerasi yang menentukan format yang diizinkan dari s
. Nilai umum yang akan ditentukan adalah Integer.
Mengembalikan
Bilangan bulat 16-bit yang tidak ditandatangani setara dengan angka yang ditentukan dalam s
.
- Atribut
Pengecualian
s
adalah null
.
style
bukan nilai NumberStyles .
-atau-
style
bukan kombinasi nilai AllowHexSpecifier dan HexNumber .
s
tidak dalam format yang sesuai dengan style
.
s
mewakili angka yang kurang dari UInt16.MinValue atau lebih besar dari UInt16.MaxValue.
-atau-
s
termasuk bukan nol, digit pecahan.
Contoh
Contoh berikut mencoba mengurai setiap elemen dalam array string dengan menggunakan sejumlah NumberStyles nilai.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.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 {
ushort number = UInt16.Parse(value, style);
Console.WriteLine(" {0}: {1}", style, number);
}
catch (FormatException) {
Console.WriteLine(" {0}: Bad Format", style);
}
}
Console.WriteLine();
}
}
}
// The example display the following output:
// Attempting to convert ' 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064
// 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 '1241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 1241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '2153.0':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 2153
//
// Attempting to convert '1e03':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 1000
//
// Attempting to convert '1300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 13
open System
open System.Globalization
let values = [| " 214 "; "1,064"; "(0)"; "1241+"; " + 214 "; " +214 "; "2153.0"; "1e03"; "1300.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 = UInt16.Parse(value, style)
printfn $" {style}: {number}"
with :? FormatException ->
printfn $" {style}: Bad Format"
printfn ""
// The example display the following output:
// Attempting to convert ' 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064
// 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 '1241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 1241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '2153.0':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 2153
//
// Attempting to convert '1e03':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 1000
//
// Attempting to convert '1300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 13
Imports System.Globalization
Module Example
Public Sub Main()
Dim values() As String = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.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 UShort = UInt16.Parse(value, style)
Console.WriteLine(" {0}: {1}", style, number)
Catch e As FormatException
Console.WriteLine(" {0}: Bad Format", style)
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Attempting to convert ' 214 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: 214
' Integer, AllowTrailingSign: 214
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '1,064':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: 1064
' 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 '1241+':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: 1241
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert ' + 214 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert ' +214 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: 214
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '2153.0':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 2153
'
' Attempting to convert '1e03':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 1000
'
' Attempting to convert '1300.0e-2':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 13
Keterangan
Parameter style
menentukan elemen gaya (seperti spasi putih, simbol tanda positif atau negatif, simbol pemisah grup, atau simbol titik desimal) yang diizinkan dalam s
parameter agar operasi penguraian berhasil.
style
harus berupa kombinasi bendera bit dari NumberStyles enumerasi. Parameter style
membuat metode ini kelebihan beban berguna ketika s
berisi representasi string dari nilai heksadesimal, ketika sistem angka (desimal atau heksadesimal) yang diwakili oleh s
hanya diketahui pada durasi, atau ketika Anda ingin melarang spasi putih atau simbol tanda di s
.
Bergantung pada nilai style
, s
parameter dapat mencakup elemen berikut:
[ws] [$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
Elemen dalam tanda kurung siku ([ dan ]) adalah opsional. Jika style
menyertakan NumberStyles.AllowHexSpecifier, s
parameter mungkin berisi elemen berikut:
[ws] hexdigits[ws]
Tabel berikut ini menjelaskan setiap elemen.
Elemen | Deskripsi |
---|---|
Ws | Spasi kosong opsional. Spasi kosong dapat muncul di awal s jika style menyertakan NumberStyles.AllowLeadingWhite bendera, dan dapat muncul di akhir s jika style menyertakan NumberStyles.AllowTrailingWhite bendera. |
$ | Simbol mata uang khusus budaya. Posisinya dalam string didefinisikan oleh NumberFormatInfo.CurrencyNegativePattern properti dan NumberFormatInfo.CurrencyPositivePattern dari budaya saat ini. Simbol mata uang budaya saat ini dapat muncul jika s style menyertakan NumberStyles.AllowCurrencySymbol bendera . |
sign | Tanda opsional. Tanda dapat muncul di awal s jika style menyertakan NumberStyles.AllowLeadingSign bendera, dan dapat muncul di akhir s jika style menyertakan NumberStyles.AllowTrailingSign bendera. Tanda kurung dapat digunakan s untuk menunjukkan nilai negatif jika style menyertakan NumberStyles.AllowParentheses bendera . Namun, simbol tanda negatif hanya dapat digunakan dengan nol; jika tidak, metode melempar OverflowException. |
Digit fractional_digits exponential_digits |
Urutan digit dari 0 hingga 9. Untuk fractional_digits, hanya digit 0 yang valid. |
, | Simbol pemisah grup khusus budaya. Pemisah grup budaya saat ini dapat muncul jika s style menyertakan NumberStyles.AllowThousands bendera . |
. | Simbol titik desimal khusus budaya. Simbol titik desimal budaya saat ini dapat muncul jika s style menyertakan NumberStyles.AllowDecimalPoint bendera . Hanya digit 0 yang dapat muncul sebagai digit pecahan agar operasi penguraian berhasil; jika fractional_digits menyertakan digit lain, akan FormatException dilemparkan. |
E | Karakter "e" atau "E", yang menunjukkan bahwa nilai diwakili dalam notasi eksponensial (ilmiah). Parameter s dapat mewakili angka dalam notasi eksponensial jika style menyertakan NumberStyles.AllowExponent bendera . |
hexdigits | Urutan digit heksadesimal dari 0 hingga f, atau 0 hingga F. |
Catatan
Setiap karakter NUL yang mengakhiri (U+0000) di s
diabaikan oleh operasi penguraian, terlepas dari style
nilai argumen.
String dengan digit saja (yang sesuai dengan NumberStyles.None gaya) selalu berhasil diurai jika berada dalam rentang UInt16 jenis. Sebagian besar elemen kontrol anggota yang tersisa NumberStyles yang mungkin ada, tetapi tidak diharuskan ada, dalam string input. Tabel berikut menunjukkan bagaimana anggota individu NumberStyles memengaruhi elemen yang mungkin ada di s
.
NumberStyles nilai |
Elemen yang diizinkan selain s digit |
---|---|
None | Elemen digit saja. |
AllowDecimalPoint | Elemen titik desimal (.) dan fractional-digits . |
AllowExponent | Karakter "e" atau "E", yang menunjukkan notasi eksponensial, bersama dengan exponential_digits. |
AllowLeadingWhite | Elemen ws di awal s . |
AllowTrailingWhite | Elemen ws di akhir s . |
AllowLeadingSign | Elemen tanda di awal s . |
AllowTrailingSign | Elemen tanda di akhir s . |
AllowParentheses | Elemen tanda dalam bentuk tanda kurung yang menyertakan nilai numerik. |
AllowThousands | Elemen pemisah grup (,). |
AllowCurrencySymbol | Elemen mata uang ($). |
Currency | Semua elemen. Namun, s tidak dapat mewakili angka heksadesimal atau angka dalam notasi eksponensial. |
Float | Elemen ws di awal atau akhir , s tanda di awal s , dan simbol titik desimal (.). Parameter s juga dapat menggunakan notasi eksponensial. |
Number | Elemen ws , sign , pemisah grup (,), dan desimal (.). |
Any | Semua elemen. Namun, s tidak dapat mewakili angka heksadesimal. |
Tidak seperti nilai lain NumberStyles , yang memungkinkan, tetapi tidak diperlukan, keberadaan elemen gaya tertentu dalam s
, NumberStyles.AllowHexSpecifier nilai gaya berarti bahwa karakter numerik individu di selalu ditafsirkan s
sebagai karakter heksadesimal. Karakter heksadesimal yang valid adalah 0-9, A-F, dan a-f. Awalan, seperti "0x", tidak didukung dan menyebabkan operasi penguraian gagal. Satu-satunya bendera lain yang dapat dikombinasikan dengan style
parameter adalah NumberStyles.AllowLeadingWhite dan NumberStyles.AllowTrailingWhite. (Enumerasi NumberStyles mencakup gaya angka komposit, NumberStyles.HexNumber, yang mencakup kedua bendera spasi putih.)
Catatan
Jika s
merupakan representasi string dari angka heksadesimal, itu tidak dapat didahului oleh dekorasi apa pun (seperti 0x
atau &h
) yang membedakannya sebagai angka heksadesimal. Hal ini menyebabkan konversi gagal.
Parameter s
diurai dengan menggunakan informasi pemformatan dalam objek yang diinisialisasi NumberFormatInfo untuk budaya sistem saat ini. Untuk menentukan budaya yang informasi pemformatannya digunakan untuk operasi penguraian, panggil Parse(String, NumberStyles, IFormatProvider) kelebihan beban.
Lihat juga
Berlaku untuk
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Mengurai rentang karakter ke dalam nilai.
public:
static System::UInt16 Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::UInt16>::Parse;
public static ushort Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> uint16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As UShort
Parameter
- s
- ReadOnlySpan<Char>
Rentang karakter untuk diurai.
- provider
- IFormatProvider
Objek yang menyediakan informasi pemformatan khusus budaya tentang s
.
Mengembalikan
Hasil penguraian s
.
Penerapan
Berlaku untuk
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Menguraikan rentang karakter UTF-8 ke dalam nilai.
public:
static System::UInt16 Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::UInt16>::Parse;
public static ushort Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> uint16
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As UShort
Parameter
- utf8Text
- ReadOnlySpan<Byte>
Rentang karakter UTF-8 untuk diurai.
- provider
- IFormatProvider
Objek yang menyediakan informasi pemformatan khusus budaya tentang utf8Text
.
Mengembalikan
Hasil penguraian utf8Text
.
Penerapan
Berlaku untuk
Parse(String)
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
- Sumber:
- UInt16.cs
Mengonversi representasi string angka ke bilangan bulat tidak bertanda 16-bit yang setara.
public:
static System::UInt16 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static ushort Parse (string s);
public static ushort Parse (string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint16
static member Parse : string -> uint16
Public Shared Function Parse (s As String) As UShort
Parameter
- s
- String
String yang mewakili angka yang akan dikonversi.
Mengembalikan
Bilangan bulat 16-bit yang tidak ditandatangani setara dengan angka yang terkandung dalam s
.
- Atribut
Pengecualian
s
adalah null
.
s
tidak dalam format yang benar.
s
mewakili angka yang kurang dari UInt16.MinValue atau lebih besar dari UInt16.MaxValue.
Contoh
Contoh berikut memanggil Parse(String) metode untuk mengonversi setiap elemen dalam array string menjadi bilangan bulat 16-bit yang tidak ditandatangani.
using System;
public class Example
{
public static void Main()
{
string[] values = { "-0", "17", "-12", "185", "66012", "+0",
"", null, "16.1", "28.0", "1,034" };
foreach (string value in values)
{
try {
ushort number = UInt16.Parse(value);
Console.WriteLine("'{0}' --> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("'{0}' --> Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("'{0}' --> OverflowException", value);
}
catch (ArgumentNullException) {
Console.WriteLine("'{0}' --> Null", value);
}
}
}
}
// The example displays the following output:
// '-0' --> 0
// '17' --> 17
// '-12' --> OverflowException
// '185' --> 185
// '66012' --> OverflowException
// '+0' --> 0
// '' --> Bad Format
// '' --> Null
// '16.1' --> Bad Format
// '28.0' --> Bad Format
// '1,034' --> Bad Format
open System
let values =
[| "-0"; "17"; "-12"; "185"; "66012"; "+0"
""; null; "16.1"; "28.0"; "1,034" |]
for value in values do
try
let number = UInt16.Parse value
printfn $"'{value}' --> {number}"
with
| :? FormatException ->
printfn $"'{value}' --> Bad Format"
| :? OverflowException ->
printfn $"'{value}' --> OverflowException"
| :? ArgumentNullException ->
printfn $"'{value}' --> Null"
// The example displays the following output:
// '-0' --> 0
// '17' --> 17
// '-12' --> OverflowException
// '185' --> 185
// '66012' --> OverflowException
// '+0' --> 0
// '' --> Bad Format
// '' --> Null
// '16.1' --> Bad Format
// '28.0' --> Bad Format
// '1,034' --> Bad Format
Module Example
Public Sub Main()
Dim values() As String = { "-0", "17", "-12", "185", "66012", _
"+0", "", Nothing, "16.1", "28.0", _
"1,034" }
For Each value As String In values
Try
Dim number As UShort = UInt16.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}' --> OverflowException", value)
Catch e As ArgumentNullException
Console.WriteLine("'{0}' --> Null", value)
End Try
Next
End Sub
End Module
' The example displays the following output:
' '-0' --> 0
' '17' --> 17
' '-12' --> OverflowException
' '185' --> 185
' '66012' --> OverflowException
' '+0' --> 0
' '' --> Bad Format
' '' --> Null
' '16.1' --> Bad Format
' '28.0' --> Bad Format
' '1,034' --> Bad Format
Keterangan
Parameter s
harus menjadi representasi string dari angka dalam formulir berikut.
[ws] [tanda]digits[ws]
Elemen dalam tanda kurung siku ([ dan ]) adalah opsional. Tabel berikut ini menjelaskan setiap elemen.
Elemen | Deskripsi |
---|---|
Ws | Spasi kosong opsional. |
sign | Tanda opsional. Karakter tanda yang valid ditentukan oleh NumberFormatInfo.NegativeSign properti dan NumberFormatInfo.PositiveSign dari budaya saat ini. Namun, simbol tanda negatif hanya dapat digunakan dengan nol; jika tidak, metode melempar OverflowException. |
Digit | Urutan digit mulai dari 0 hingga 9. Setiap nol di depan diabaikan. |
Catatan
String yang ditentukan oleh s
parameter ditafsirkan dengan menggunakan NumberStyles.Integer gaya . Ini tidak boleh berisi pemisah grup atau pemisah desimal, dan tidak dapat memiliki bagian desimal.
Parameter s
diurai dengan menggunakan informasi pemformatan dalam objek yang diinisialisasi System.Globalization.NumberFormatInfo untuk budaya sistem saat ini. Untuk informasi selengkapnya, lihat NumberFormatInfo.CurrentInfo. Untuk mengurai string dengan menggunakan informasi pemformatan budaya tertentu, gunakan metode .Parse(String, IFormatProvider)
Lihat juga
Berlaku untuk
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk