UInt16.Parse Methode

Definitie

Converteert de tekenreeksweergave van een getal naar het 16-bits equivalent van een niet-ondertekend geheel getal.

Overloads

Name Description
Parse(String, NumberStyles, IFormatProvider)

Converteert de tekenreeksweergave van een getal in een opgegeven stijl en cultuurspecifieke notatie naar het 16-bits equivalent van niet-ondertekende gehele getallen.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Converteert de spanweergave van een getal in een opgegeven stijl en cultuurspecifieke indeling naar het 16-bits equivalent van niet-ondertekende gehele getallen.

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Parseert een reeks UTF-8 tekens in een waarde.

Parse(String, IFormatProvider)

Converteert de tekenreeksweergave van een getal in een opgegeven cultuurspecifieke notatie naar het 16-bits equivalent van een niet-ondertekend geheel getal.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Parseert een reeks tekens in een waarde.

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Parseert een reeks UTF-8 tekens in een waarde.

Parse(String)

Converteert de tekenreeksweergave van een getal naar het 16-bits equivalent van een niet-ondertekend geheel getal.

Parse(String, NumberStyles)

Converteert de tekenreeksweergave van een getal in een opgegeven stijl naar het 16-bits equivalent van het niet-ondertekende gehele getal.

Deze methode is niet cls-compatibel. Het cls-compatibele alternatief is Parse(String, NumberStyles).

Parse(String, NumberStyles, IFormatProvider)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Belangrijk

Deze API is niet CLS-conform.

CLS-conform alternatief
System.Int32.Parse(String)

Converteert de tekenreeksweergave van een getal in een opgegeven stijl en cultuurspecifieke notatie naar het 16-bits equivalent van niet-ondertekende gehele getallen.

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

Parameters

s
String

Een tekenreeks die het getal vertegenwoordigt dat moet worden geconverteerd. De tekenreeks wordt geïnterpreteerd met behulp van de stijl die is opgegeven door de style parameter.

style
NumberStyles

Een bitsgewijze combinatie van opsommingswaarden die aangeven in welke stijlelementen aanwezig kunnen zijn s. Een typische waarde die moet worden opgegeven, is Integer.

provider
IFormatProvider

Een object dat cultuurspecifieke opmaakinformatie over slevert.

Retouren

Een 16-bits geheel getal zonder teken dat gelijk is aan het getal dat is opgegeven in s.

Implementeringen

Kenmerken

Uitzonderingen

style is geen NumberStyles waarde.

– of –

style is geen combinatie van AllowHexSpecifier en HexNumber waarden.

s heeft geen indeling die compatibel is met style.

s vertegenwoordigt een getal dat kleiner is dan UInt16.MinValue of groter dan UInt16.MaxValue.

– of –

s bevat niet-nul, fractionele cijfers.

Voorbeelden

In het volgende voorbeeld wordt de Parse(String, NumberStyles, IFormatProvider) methode gebruikt om verschillende tekenreeksweergaven van getallen te converteren naar 16-bits niet-ondertekende gehele getallen.

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.

Opmerkingen

De style parameter definieert de stijlelementen (zoals witruimte of het positieve of negatieve tekensymbool) die zijn toegestaan in de s parameter om de parseringsbewerking te laten slagen. Dit moet een combinatie zijn van bitvlagmen uit de NumberStyles opsomming.

Afhankelijk van de waarde van style, kan de s parameter de volgende elementen bevatten:

[ws][$][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]

Elementen in vierkante haken ([ en ]) zijn optioneel. Als style dit is opgenomen NumberStyles.AllowHexSpecifier, kan de s parameter de volgende elementen bevatten:

[ws]hexdigits[ws]

In de volgende tabel wordt elk element beschreven.

Element Beschrijving
Ws Optionele witruimte. Witruimte kan aan het begin van s de styleNumberStyles.AllowLeadingWhite vlag worden weergegeven en deze kan aan het einde van s de styleNumberStyles.AllowTrailingWhite vlag worden weergegeven.
$ Een cultuurspecifiek valutasymbool. De positie in de tekenreeks wordt gedefinieerd door de CurrencyPositivePattern eigenschap van het NumberFormatInfo object dat wordt geretourneerd door de GetFormat methode van de provider parameter. Het valutasymbool kan worden weergegeven s als style deze de NumberStyles.AllowCurrencySymbol vlag bevat.
ondertekenen Een optioneel teken. (De methode genereert een OverflowException if-teken s met een negatief teken en vertegenwoordigt een getal dat niet nul is.) Het teken kan aan het begin van s de vlag worden weergegeven, styleNumberStyles.AllowLeadingSign en het kan het einde van s de vlag weergeven als style deze de NumberStyles.AllowTrailingSign vlag bevat. Haakjes kunnen worden gebruikt s om een negatieve waarde aan te geven als style deze de NumberStyles.AllowParentheses vlag bevat.
Cijfers Een reeks cijfers van 0 tot en met 9.
. Een cultuurspecifiek decimaalteken. Het decimale puntsymbool van de huidige cultuur kan worden weergegeven s als style deze de NumberStyles.AllowDecimalPoint vlag bevat.
fractional_digits Een of meer exemplaren van het cijfer 0-9 als style deze de NumberStyles.AllowExponent vlag bevat, of een of meer exemplaren van het cijfer 0 als dat niet het geval is. Fractionele cijfers kunnen alleen worden weergegeven als sstyle de NumberStyles.AllowDecimalPoint vlag wordt opgenomen.
E Het teken "e" of "E", dat aangeeft dat de waarde wordt weergegeven in exponentiële (wetenschappelijke) notatie. De s parameter kan een getal in exponentiële notatie vertegenwoordigen als style deze de NumberStyles.AllowExponent vlag bevat.
exponential_digits Een reeks cijfers van 0 tot en met 9. De s parameter kan een getal in exponentiële notatie vertegenwoordigen als style deze de NumberStyles.AllowExponent vlag bevat.
hexdigits Een reeks hexadecimale cijfers van 0 tot en met f of 0 tot en met F.

Note

Alle nultekens (U+0000) s worden genegeerd door de parseringsbewerking, ongeacht de waarde van het style argument.

Een tekenreeks met alleen decimale cijfers (die overeenkomt met de NumberStyles.None stijl) wordt altijd geparseerd. De meeste resterende NumberStyles leden bepalen elementen die mogelijk aanwezig zijn, maar die niet aanwezig zijn, in deze invoertekenreeks. De volgende tabel geeft aan hoe afzonderlijke NumberStyles leden van invloed zijn op de elementen die aanwezig kunnen zijn in s.

Niet-samengestelde NumberStyles waarden Elementen die naast cijfers zijn toegestaan s
NumberStyles.None Alleen decimale cijfers.
NumberStyles.AllowDecimalPoint De decimale punt (.) en fractional_digits elementen. Als de stijl echter niet de NumberStyles.AllowExponent vlag bevat, moet fractional_digits uit slechts één of meer cijfers bestaan; anders wordt er een OverflowException gegenereerd.
NumberStyles.AllowExponent Het teken "e" of "E", dat exponentiële notatie aangeeft, samen met exponential_digits.
NumberStyles.AllowLeadingWhite Het ws-element aan het begin van s.
NumberStyles.AllowTrailingWhite Het ws-element aan het einde van s.
NumberStyles.AllowLeadingSign Een teken voor cijfers.
NumberStyles.AllowTrailingSign Een teken na cijfers.
NumberStyles.AllowParentheses Haakjes voor en na cijfers om een negatieve waarde aan te geven.
NumberStyles.AllowThousands Het groepsscheidingsteken (,) element.
NumberStyles.AllowCurrencySymbol Het valutaelement ($).

Als de NumberStyles.AllowHexSpecifier vlag wordt gebruikt, s moet dit een hexadecimale waarde zijn. Geldige hexadecimale cijfers zijn 0 tot en met 9, een tot en met f en A tot en met F. Een voorvoegsel, zoals '0x', wordt niet ondersteund en zorgt ervoor dat de parseringsbewerking mislukt. De enige andere vlaggen die kunnen worden gecombineerd met NumberStyles.AllowHexSpecifier zijn NumberStyles.AllowLeadingWhite en NumberStyles.AllowTrailingWhite. (De NumberStyles opsomming bevat een samengestelde getalstijl, NumberStyles.HexNumberdie beide spatievlagmen bevat.)

Note

Als de parameter de s tekenreeksweergave is van een hexadecimaal getal, kan deze niet worden voorafgegaan door een decoratie (zoals 0x of &h) die deze onderscheidt als een hexadecimaal getal. Dit zorgt ervoor dat de parseringsbewerking een uitzondering genereert.

De provider parameter is een IFormatProvider implementatie waarvan de GetFormat methode een NumberFormatInfo object retourneert dat cultuurspecifieke informatie biedt over de indeling van s. Er zijn drie manieren om de provider parameter te gebruiken om aangepaste opmaakgegevens op te geven voor de parseringsbewerking:

  • U kunt het werkelijke NumberFormatInfo object doorgeven dat opmaakinformatie biedt. (De implementatie van GetFormat simpelweg retourneert zichzelf.)

  • U kunt een CultureInfo object doorgeven dat de cultuur aangeeft waarvan de opmaak moet worden gebruikt. De NumberFormat eigenschap bevat opmaakgegevens.

  • U kunt een aangepaste IFormatProvider implementatie doorgeven. De GetFormat methode moet het object instantiëren en retourneren NumberFormatInfo dat opmaakinformatie biedt.

Als provider dat het is null, wordt het NumberFormatInfo object voor de huidige cultuur gebruikt.

Zie ook

Van toepassing op

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Belangrijk

Deze API is niet CLS-conform.

Converteert de spanweergave van een getal in een opgegeven stijl en cultuurspecifieke indeling naar het 16-bits equivalent van niet-ondertekende gehele getallen.

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

Parameters

s
ReadOnlySpan<Char>

Een bereik met de tekens die het getal vertegenwoordigen dat moet worden geconverteerd. De periode wordt geïnterpreteerd met behulp van de stijl die is opgegeven door de style parameter.

style
NumberStyles

Een bitsgewijze combinatie van opsommingswaarden die aangeven in welke stijlelementen aanwezig kunnen zijn s. Een typische waarde die moet worden opgegeven, is Integer.

provider
IFormatProvider

Een object dat cultuurspecifieke opmaakinformatie over slevert.

Retouren

Een 16-bits geheel getal zonder teken dat gelijk is aan het getal dat is opgegeven in s.

Implementeringen

Kenmerken

Van toepassing op

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Parseert een reeks UTF-8 tekens in een waarde.

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

Parameters

utf8Text
ReadOnlySpan<Byte>

Het bereik van UTF-8 tekens om te parseren.

style
NumberStyles

Een bitsgewijze combinatie van getalstijlen die aanwezig kunnen zijn in utf8Text.

provider
IFormatProvider

Een object dat cultuurspecifieke opmaakinformatie biedt over utf8Text.

Retouren

Het resultaat van parseren utf8Text.

Implementeringen

Van toepassing op

Parse(String, IFormatProvider)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Belangrijk

Deze API is niet CLS-conform.

CLS-conform alternatief
System.Int32.Parse(String)

Converteert de tekenreeksweergave van een getal in een opgegeven cultuurspecifieke notatie naar het 16-bits equivalent van een niet-ondertekend geheel getal.

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

Parameters

s
String

Een tekenreeks die het getal vertegenwoordigt dat moet worden geconverteerd.

provider
IFormatProvider

Een object dat cultuurspecifieke opmaakinformatie over slevert.

Retouren

Een 16-bits geheel getal zonder teken dat gelijk is aan het getal dat is opgegeven in s.

Implementeringen

Kenmerken

Uitzonderingen

s heeft niet de juiste indeling.

s vertegenwoordigt een getal kleiner dan UInt16.MinValue of groter dan UInt16.MaxValue.

Voorbeelden

In het volgende voorbeeld wordt een aangepaste cultuur geïnstitueert die gebruikmaakt van twee plustekens (++) als positief teken. Vervolgens wordt de Parse(String, IFormatProvider) methode aangeroepen om een matrix met tekenreeksen te parseren met behulp van CultureInfo objecten die zowel deze aangepaste cultuur als de invariante cultuur vertegenwoordigen.

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.

Opmerkingen

De s parameter bevat een getal van het formulier:

[ws][teken]digits[ws]

Items in vierkante haken ([ en ]) zijn optioneel. In de volgende tabel wordt elk element beschreven.

Element Beschrijving
Ws Optionele witruimte.
sign Een optioneel teken of een negatief teken als s de waarde nul aangeeft.
Cijfers Een reeks cijfers tussen 0 en 9.

De parameter s wordt geïnterpreteerd met behulp van de NumberStyles.Integer stijl. Naast de decimale cijfers van de bytewaarde zijn alleen voorloop- en volgspaties met een voorloopteken toegestaan. (Als het negatieve teken aanwezig is, s moet u een waarde van nul vertegenwoordigen of de methode een OverflowException.) Als u de stijlelementen expliciet wilt definiëren in combinatie met de cultuurspecifieke opmaakinformatie die aanwezig kan zijn s, gebruikt u de Parse(String, NumberStyles, IFormatProvider) methode.

De provider parameter is een IFormatProvider implementatie waarvan de GetFormat methode een NumberFormatInfo object retourneert dat cultuurspecifieke informatie biedt over de indeling van s. Er zijn drie manieren om de provider parameter te gebruiken om aangepaste opmaakgegevens op te geven voor de parseringsbewerking:

  • U kunt het werkelijke NumberFormatInfo object doorgeven dat opmaakinformatie biedt. (De implementatie van GetFormat simpelweg retourneert zichzelf.)

  • U kunt een CultureInfo object doorgeven dat de cultuur aangeeft waarvan de opmaak moet worden gebruikt. De NumberFormat eigenschap bevat opmaakgegevens.

  • U kunt een aangepaste IFormatProvider implementatie doorgeven. De GetFormat methode moet het object instantiëren en retourneren NumberFormatInfo dat opmaakinformatie biedt.

Als provider dat het is null, wordt de NumberFormatInfo voor de huidige cultuur gebruikt.

Zie ook

Van toepassing op

Parse(ReadOnlySpan<Char>, IFormatProvider)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Parseert een reeks tekens in een waarde.

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

Parameters

s
ReadOnlySpan<Char>

De reeks tekens die moeten worden geparseerd.

provider
IFormatProvider

Een object dat cultuurspecifieke opmaakinformatie biedt over s.

Retouren

Het resultaat van parseren s.

Implementeringen

Van toepassing op

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Parseert een reeks UTF-8 tekens in een waarde.

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

Parameters

utf8Text
ReadOnlySpan<Byte>

Het bereik van UTF-8 tekens om te parseren.

provider
IFormatProvider

Een object dat cultuurspecifieke opmaakinformatie biedt over utf8Text.

Retouren

Het resultaat van parseren utf8Text.

Implementeringen

Van toepassing op

Parse(String)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Belangrijk

Deze API is niet CLS-conform.

CLS-conform alternatief
System.Int32.Parse(String)

Converteert de tekenreeksweergave van een getal naar het 16-bits equivalent van een niet-ondertekend geheel getal.

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

Parameters

s
String

Een tekenreeks die het getal vertegenwoordigt dat moet worden geconverteerd.

Retouren

Een 16-bits geheel getal zonder teken dat gelijk is aan het getal in s.

Kenmerken

Uitzonderingen

s heeft niet de juiste indeling.

s vertegenwoordigt een getal kleiner dan UInt16.MinValue of groter dan UInt16.MaxValue.

Voorbeelden

In het volgende voorbeeld wordt de Parse(String) methode aangeroepen om elk element in een tekenreeksmatrix te converteren naar een niet-ondertekend 16-bits geheel getal.

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

Opmerkingen

De s parameter moet de tekenreeksweergave van een getal zijn in de volgende vorm.

[ws][teken]digits[ws]

Elementen in vierkante haken ([ en ]) zijn optioneel. In de volgende tabel wordt elk element beschreven.

Element Beschrijving
Ws Optionele witruimte.
ondertekenen Een optioneel teken. Geldige tekens worden bepaald door de NumberFormatInfo.NegativeSign en NumberFormatInfo.PositiveSign eigenschappen van de huidige cultuur. Het symbool voor een negatief teken kan echter alleen worden gebruikt met nul; anders gooit de methode een OverflowException.
Cijfers Een reeks cijfers tussen 0 en 9. Voorloopnullen worden genegeerd.

Note

De tekenreeks die door de s parameter is opgegeven, wordt geïnterpreteerd met behulp van de NumberStyles.Integer stijl. Het mag geen groepsscheidingstekens of decimaalteken bevatten en kan geen decimaal gedeelte bevatten.

De s parameter wordt geparseerd met behulp van de opmaakgegevens in een System.Globalization.NumberFormatInfo object dat is geïnitialiseerd voor de huidige systeemcultuur. Zie NumberFormatInfo.CurrentInfo voor meer informatie. Als u een tekenreeks wilt parseren met behulp van de opmaakgegevens van een specifieke cultuur, gebruikt u de Parse(String, IFormatProvider) methode.

Zie ook

Van toepassing op

Parse(String, NumberStyles)

Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs
Bron:
UInt16.cs

Belangrijk

Deze API is niet CLS-conform.

Converteert de tekenreeksweergave van een getal in een opgegeven stijl naar het 16-bits equivalent van het niet-ondertekende gehele getal.

Deze methode is niet cls-compatibel. Het cls-compatibele alternatief is 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

Parameters

s
String

Een tekenreeks die het getal vertegenwoordigt dat moet worden geconverteerd. De tekenreeks wordt geïnterpreteerd met behulp van de stijl die is opgegeven door de style parameter.

style
NumberStyles

Een bitsgewijze combinatie van de opsommingswaarden die de toegestane notatie van s. Een typische waarde die moet worden opgegeven, is Integer.

Retouren

Een 16-bits geheel getal zonder teken dat gelijk is aan het getal dat is opgegeven in s.

Kenmerken

Uitzonderingen

style is geen NumberStyles waarde.

– of –

style is geen combinatie van AllowHexSpecifier en HexNumber waarden.

s heeft geen indeling die compatibel is met style.

s vertegenwoordigt een getal kleiner dan UInt16.MinValue of groter dan UInt16.MaxValue.

– of –

s bevat niet-nul, fractionele cijfers.

Voorbeelden

In het volgende voorbeeld wordt geprobeerd om elk element in een tekenreeksmatrix te parseren met behulp van NumberStyles een aantal waarden.

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

Opmerkingen

De style parameter definieert de stijlelementen (zoals witruimte, het positieve of negatieve tekensymbool, het teken voor het groepsscheidingsteken of het decimale puntsymbool) die zijn toegestaan in de s parameter om de parseringsbewerking te laten slagen. style moet een combinatie zijn van bitvlagmen uit de NumberStyles opsomming. De style parameter maakt deze methode overbelast wanneer s de tekenreeksweergave van een hexadecimale waarde bevat, wanneer het getalsysteem (decimaal of hexadecimaal) dat wordt vertegenwoordigd s door alleen tijdens runtime bekend is, of wanneer u witruimte of een tekensymbool swilt weigeren.

Afhankelijk van de waarde van style, kan de s parameter de volgende elementen bevatten:

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

Elementen in vierkante haken ([ en ]) zijn optioneel. Indien style opgenomen NumberStyles.AllowHexSpecifier, kan de s parameter de volgende elementen bevatten:

[ws]hexdigits[ws]

In de volgende tabel wordt elk element beschreven.

Element Beschrijving
Ws Optionele witruimte. Witruimte kan aan het begin van s de styleNumberStyles.AllowLeadingWhite vlag worden weergegeven en kan aan het einde van s de vlag worden weergegeven.styleNumberStyles.AllowTrailingWhite
$ Een cultuurspecifiek valutasymbool. De positie in de tekenreeks wordt gedefinieerd door de NumberFormatInfo.CurrencyNegativePattern en NumberFormatInfo.CurrencyPositivePattern eigenschappen van de huidige cultuur. Het valutasymbool van de huidige cultuur kan worden weergegeven als sstyle deze de NumberStyles.AllowCurrencySymbol vlag bevat.
ondertekenen Een optioneel teken. Het teken kan aan het begin van s de vlag worden weergegeven als style deze de NumberStyles.AllowLeadingSign vlag bevat en kan worden weergegeven aan het einde van s de vlag.styleNumberStyles.AllowTrailingSign Haakjes kunnen worden gebruikt s om een negatieve waarde aan te geven als style deze de NumberStyles.AllowParentheses vlag bevat. Het symbool voor een negatief teken kan echter alleen worden gebruikt met nul; anders gooit de methode een OverflowException.
Cijfers

fractional_digits

exponential_digits
Een reeks cijfers van 0 tot en met 9. Voor fractional_digits is alleen het cijfer 0 geldig.
, Een cultuurspecifiek groepsscheidingsteken. Het groepsscheidingsteken van de huidige cultuur kan worden weergegeven als sstyle deze de NumberStyles.AllowThousands vlag bevat.
. Een cultuurspecifiek decimaalteken. Het decimale puntsymbool van de huidige cultuur kan worden weergegeven s als style deze de NumberStyles.AllowDecimalPoint vlag bevat. Alleen het cijfer 0 kan worden weergegeven als een breukcijfer om de parseringsbewerking te laten slagen; als fractional_digits een ander cijfer bevat, wordt er een FormatException gegenereerd.
E Het teken "e" of "E", dat aangeeft dat de waarde wordt weergegeven in exponentiële (wetenschappelijke) notatie. De s parameter kan een getal in exponentiële notatie vertegenwoordigen als style deze de NumberStyles.AllowExponent vlag bevat.
hexdigits Een reeks hexadecimale cijfers van 0 tot en met f of 0 tot en met F.

Note

Alle nultekens (U+0000) s worden genegeerd door de parseringsbewerking, ongeacht de waarde van het style argument.

Een tekenreeks met alleen cijfers (die overeenkomt met de NumberStyles.None stijl) parseert altijd goed als deze zich in het bereik van het UInt16 type bevindt. De meeste resterende NumberStyles leden bepalen elementen die mogelijk aanwezig zijn, maar die niet aanwezig zijn, in de invoertekenreeks. De volgende tabel geeft aan hoe afzonderlijke NumberStyles leden van invloed zijn op de elementen die aanwezig kunnen zijn in s.

NumberStyles Waarde Elementen die naast cijfers zijn toegestaan s
None Alleen het cijferelement .
AllowDecimalPoint De decimale puntelementen (.) en fractionele cijfers .
AllowExponent Het teken "e" of "E", dat exponentiële notatie aangeeft, samen met exponential_digits.
AllowLeadingWhite Het ws-element aan het begin van s.
AllowTrailingWhite Het ws-element aan het einde van s.
AllowLeadingSign Het tekenelement aan het begin van s.
AllowTrailingSign Het tekenelement aan het einde van s.
AllowParentheses Het tekenelement in de vorm van haakjes tussen de numerieke waarde.
AllowThousands Het groepsscheidingsteken (,) element.
AllowCurrencySymbol Het valutaelement ($).
Currency Alle elementen. s Kan echter geen hexadecimaal getal of een getal in exponentiële notatie vertegenwoordigen.
Float Het ws-element aan het begin of einde van s, teken aan het begin van sen het decimaalteken (.). De s parameter kan ook exponentiële notatie gebruiken.
Number De wselementen , signgroepsscheidingsteken (,) en decimaalteken (.).
Any Alle elementen. s Kan echter geen hexadecimaal getal vertegenwoordigen.

In tegenstelling tot de andere NumberStyles waarden, die toestaan, maar niet vereisen, betekent de aanwezigheid van bepaalde stijlelementen in s, de NumberStyles.AllowHexSpecifier stijlwaarde dat de afzonderlijke numerieke tekens s altijd worden geïnterpreteerd als hexadecimale tekens. Geldige hexadecimale tekens zijn 0-9, A-F en a-f. Een voorvoegsel, zoals '0x', wordt niet ondersteund en zorgt ervoor dat de parseringsbewerking mislukt. De enige andere vlaggen die kunnen worden gecombineerd met de style parameter zijn NumberStyles.AllowLeadingWhite en NumberStyles.AllowTrailingWhite. (De NumberStyles opsomming bevat een samengestelde getalstijl, NumberStyles.HexNumberdie beide spatievlagmen bevat.)

Note

Als s dit de tekenreeksweergave is van een hexadecimaal getal, kan het niet worden voorafgegaan door een decoratie (zoals 0x of &h) die deze onderscheidt als een hexadecimaal getal. Dit zorgt ervoor dat de conversie mislukt.

De s parameter wordt geparseerd met behulp van de opmaakgegevens in een NumberFormatInfo object dat is geïnitialiseerd voor de huidige systeemcultuur. Als u de cultuur wilt opgeven waarvan de opmaakgegevens worden gebruikt voor de parseringsbewerking, roept u de Parse(String, NumberStyles, IFormatProvider) overbelasting aan.

Zie ook

Van toepassing op