UInt16.Parse 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다.
오버로드
| Parse(String, NumberStyles, IFormatProvider) |
지정된 스타일 및 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다. |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
지정된 스타일 및 문화권별 지정 형식으로 된 숫자의 범위 표현을 해당하는 16비트 부호 없는 정수로 변환합니다. |
| Parse(String, NumberStyles) |
지정된 스타일로 된 숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다. 이 메서드는 CLS 규격이 아닙니다. CLS 규격 대체 항목은 Parse(String, NumberStyles)입니다. |
| Parse(String) |
숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다. |
| Parse(String, IFormatProvider) |
지정된 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다. |
Parse(String, NumberStyles, IFormatProvider)
지정된 스타일 및 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다.
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
[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
매개 변수
- s
- String
변환할 숫자를 나타내는 문자열입니다. 이 문자열은 style 매개 변수로 지정된 스타일을 사용하여 해석됩니다.
- style
- NumberStyles
s에 나타날 수 있는 스타일 요소를 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.
- provider
- IFormatProvider
s에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.
반환
s에 지정된 수에 해당하는 16비트 부호 없는 정수입니다.
- 특성
예외
s이(가) null인 경우
s가 style 규격 형식이 아닙니다.
예제
다음 예제에서는 메서드를 사용 하 여 Parse(String, NumberStyles, IFormatProvider) 숫자의 다양 한 문자열 표현을 16 비트 부호 없는 정수 값으로 변환 합니다.
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.
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.
설명
style매개 변수는 구문 분석 작업에 성공하기 위해 매개 변수에 허용되는 스타일 요소(예: 공백 또는 양수 또는 음수 기호)를 s 정의합니다. 열거형의 비트 플래그 조합이어야 NumberStyles 합니다.
값에 따라 style s 매개 변수에 다음 요소가 포함될 수 있습니다.
[ws] [ $ ][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 에 가 포함된 경우 style NumberStyles.AllowHexSpecifier 매개 s 변수에 다음 요소가 포함될 수 있습니다.
[ws] hexdigits[ws]
다음 표에서는 각 요소에 대해 설명합니다.
| 요소 | 설명 |
|---|---|
| ws | 선택적 공백입니다. 가 플래그를 포함하는 경우 의 시작 부분에 s style 공백이 나타날 수 있으며, 가 NumberStyles.AllowLeadingWhite 플래그를 포함하는 경우 의 끝에 나타날 수 s style NumberStyles.AllowTrailingWhite 있습니다. |
| $ | 문화권별 통화 기호입니다. 문자열의 해당 위치는 매개 CurrencyPositivePattern NumberFormatInfo 변수의 메서드에 의해 반환되는 개체의 속성에 의해 GetFormat 정의됩니다. provider 에 플래그가 포함된 경우 통화 기호가 에 나타날 수 s style NumberStyles.AllowCurrencySymbol 있습니다. |
| sign | 선택적 기호입니다. 메서드는 가 OverflowException s 음수 기호를 포함하고 0이 아닌 숫자를 나타내는 경우 을 throw합니다. 가 플래그를 포함하는 경우 의 시작 부분에 s style 기호가 NumberStyles.AllowLeadingSign 나타날 수 있으며, 가 플래그를 포함하는 경우 의 s 끝부분으로 나타날 수 style NumberStyles.AllowTrailingSign 있습니다. 에 s 플래그가 포함된 경우 괄호를 사용하여 음수 값을 나타낼 수 style NumberStyles.AllowParentheses 있습니다. |
| 숫자 | 0에서 9까지의 숫자 시퀀스입니다. |
| . | 문화권별 소수점 기호입니다. 에 플래그가 포함된 경우 현재 문화권의 소수점 기호가 에 나타날 수 s style NumberStyles.AllowDecimalPoint 있습니다. |
| fractional_digits | 에 플래그가 포함된 경우 숫자 0-9가 하나 이상 style NumberStyles.AllowExponent 발생하거나, 그렇지 않으면 숫자 0이 하나 이상 발생합니다. 에 플래그가 포함된 경우에만 소수 자릿수가 에 나타날 수 s style NumberStyles.AllowDecimalPoint 있습니다. |
| E | 값이 지수(과학적) 표기법으로 표시되었음을 나타내는 "e" 또는 "E" 문자입니다. s에 플래그가 포함된 경우 매개 변수는 지수 표기법으로 숫자를 나타낼 수 style NumberStyles.AllowExponent 있습니다. |
| exponential_digits | 0에서 9까지의 숫자 시퀀스입니다. s에 플래그가 포함된 경우 매개 변수는 지수 표기법으로 숫자를 나타낼 수 style NumberStyles.AllowExponent 있습니다. |
| hexdigits | 0부터 f까지 또는 0부터 F까지의 16진수 시퀀스입니다. |
참고
의 종결 NUL(U+0000) s 문자는 인수 값에 관계없이 구문 분석 작업에서 style 무시됩니다.
10진수만 있는 NumberStyles.None 문자열(스타일에 해당)은 항상 성공적으로 구문 분석됩니다. 나머지 멤버의 대부분은 NumberStyles 이 입력 문자열에 존재할 수 있지만 존재할 필요는 없는 요소를 제어합니다. 다음 표에서는 개별 NumberStyles 멤버가 에 있을 수 있는 요소에 미치는 영향을 s 나타냅니다.
비 복합 NumberStyles 값 |
숫자 외에 에서 허용되는 요소 s |
|---|---|
| NumberStyles.None | 10진수만 해당합니다. |
| NumberStyles.AllowDecimalPoint | 소수점(.) 및 fractional_digits 요소입니다. 그러나 스타일에 플래그가 포함되지 않은 경우 NumberStyles.AllowExponent fractional_digits 하나 이상의 0자리 숫자로만 구성되어야 합니다. 그렇지 않으면 OverflowException 이 throw됩니다. |
| NumberStyles.AllowExponent | 지수 표기법과 함께 exponential_digits 나타내는 "e" 또는 "E" 문자입니다. |
| NumberStyles.AllowLeadingWhite | 의 시작 부분에 있는 ws s 요소입니다. |
| NumberStyles.AllowTrailingWhite | 끝에 있는 ws s 요소입니다. |
| NumberStyles.AllowLeadingSign | 숫자 앞에 부호가 있습니다. |
| NumberStyles.AllowTrailingSign | 숫자 뒤의 기호입니다. |
| NumberStyles.AllowParentheses | 숫자 앞과 뒤의 괄호를 괄호로 하여 음수 값을 나타냅니다. |
| NumberStyles.AllowThousands | 그룹 구분 기호(,) 요소입니다. |
| NumberStyles.AllowCurrencySymbol | currency ( $ ) 요소입니다. |
플래그를 사용하는 경우 NumberStyles.AllowHexSpecifier 는 s 16진수 값이어야 합니다. 유효한 16진수는 0~9, a~f, A~F입니다. 접두사(예: "0x")는 지원되지 않으며 구문 분석 작업이 실패합니다. 와 함께 사용할 수 있는 다른 플래그는 NumberStyles.AllowHexSpecifier 및 뿐 NumberStyles.AllowLeadingWhite 입니다 NumberStyles.AllowTrailingWhite . 열거형에는 NumberStyles NumberStyles.HexNumber 공백 플래그를 모두 포함 하는 복합 숫자 스타일이 포함 되어 있습니다.
참고
s매개 변수가 16 진수의 문자열 표현인 경우 16 진수를 구분 하는 데코레이션 (예: 또는) 뒤에 올 수 없습니다 0x &h . 이렇게 하면 구문 분석 작업에서 예외가 throw 됩니다.
provider매개 변수는 IFormatProvider GetFormat 메서드가 NumberFormatInfo 의 형식에 대 한 문화권별 정보를 제공 하는 개체를 반환 하는 구현입니다 s . 매개 변수를 사용 하 여 provider 구문 분석 작업에 사용자 지정 서식 지정 정보를 제공 하는 방법에는 세 가지가 있습니다.
NumberFormatInfo서식 지정 정보를 제공 하는 실제 개체를 전달할 수 있습니다. (의 구현은 GetFormat 단순히 자신을 반환 합니다.)
CultureInfo서식 지정을 사용할 문화권을 지정 하는 개체를 전달할 수 있습니다. 해당 NumberFormat 속성은 형식 지정 정보를 제공 합니다.
사용자 지정 구현을 전달할 수 있습니다 IFormatProvider . 해당 GetFormat 메서드는 NumberFormatInfo 형식 지정 정보를 제공 하는 개체를 인스턴스화하고 반환 해야 합니다.
provider가 이면 null NumberFormatInfo 현재 문화권에 대 한 개체가 사용 됩니다.
추가 정보
적용 대상
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
중요
이 API는 CLS 규격이 아닙니다.
지정된 스타일 및 문화권별 지정 형식으로 된 숫자의 범위 표현을 해당하는 16비트 부호 없는 정수로 변환합니다.
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
매개 변수
- s
- ReadOnlySpan<Char>
변환할 숫자를 나타내는 문자를 포함하는 범위입니다. style 매개 변수로 지정된 스타일을 사용하여 해석하는 범위입니다.
- style
- NumberStyles
s에 나타날 수 있는 스타일 요소를 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.
- provider
- IFormatProvider
s에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.
반환
s에 지정된 수에 해당하는 16비트 부호 없는 정수입니다.
- 특성
적용 대상
Parse(String, NumberStyles)
중요
이 API는 CLS 규격이 아닙니다.
지정된 스타일로 된 숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다.
이 메서드는 CLS 규격이 아닙니다. CLS 규격 대체 항목은 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
매개 변수
- s
- String
변환할 숫자를 나타내는 문자열입니다. 이 문자열은 style 매개 변수로 지정된 스타일을 사용하여 해석됩니다.
- style
- NumberStyles
s에 사용할 수 있는 서식을 지정하는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.
반환
s에 지정된 수에 해당하는 16비트 부호 없는 정수입니다.
- 특성
예외
s이(가) null인 경우
s가 style 규격 형식이 아닙니다.
예제
다음 예제에서는 여러 값을 사용하여 문자열 배열의 각 요소를 구문 분석하려고 NumberStyles 시도합니다.
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
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
설명
style매개 변수는 구문 분석 작업에 성공하기 위해 매개 변수에 허용되는 스타일 요소(예: 공백, 양수 또는 음수 기호, 그룹 구분 기호 또는 소수점 기호)를 s 정의합니다. style 는 열거형의 비트 플래그 조합이어야 NumberStyles 합니다. style매개 변수는 가 16진수 s 값의 문자열 표현을 포함하는 경우, 에서 나타내는 숫자 시스템(10진수 또는 16진수)이 런타임에만 알려진 경우 또는 에서 공백 또는 기호 기호를 허용되지 않으려는 경우에 이 메서드 오버로드를 유용하게 s s 만듭니다.
값에 따라 style s 매개 변수에 다음 요소가 포함될 수 있습니다.
[ws] [ $ ][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 에 가 포함된 경우 style NumberStyles.AllowHexSpecifier 매개 s 변수에 다음 요소가 포함될 수 있습니다.
[ws] hexdigits[ws]
다음 표에서는 각 요소에 대해 설명합니다.
| 요소 | 설명 |
|---|---|
| ws | 선택적 공백입니다. 가 플래그를 포함하는 경우 의 시작 부분에 s style 공백이 나타날 수 있으며, 가 NumberStyles.AllowLeadingWhite 플래그를 포함하는 경우 의 끝에 나타날 수 s style NumberStyles.AllowTrailingWhite 있습니다. |
| $ | 문화권별 통화 기호입니다. 문자열의 해당 위치는 현재 NumberFormatInfo.CurrencyNegativePattern 문화권의 및 속성에 의해 NumberFormatInfo.CurrencyPositivePattern 정의됩니다. 에 플래그가 포함된 경우 현재 문화권의 통화 기호가 에 나타날 수 s style NumberStyles.AllowCurrencySymbol 있습니다. |
| sign | 선택적 기호입니다. 가 플래그를 포함하는 경우 의 시작 부분에 s style 기호가 나타날 수 있으며, 가 NumberStyles.AllowLeadingSign 플래그를 포함하는 경우 의 끝에 표시할 수 s style NumberStyles.AllowTrailingSign 있습니다. 에 s 플래그가 포함된 경우 괄호를 사용하여 음수 값을 나타낼 수 style NumberStyles.AllowParentheses 있습니다. 그러나 음수 기호는 0에서만 사용할 수 있습니다. 그렇지 않으면 메서드가 을 OverflowException throw합니다. |
| 숫자 fractional_digits exponential_digits |
0에서 9까지의 숫자 시퀀스입니다. fractional_digits 의 경우 숫자 0만 유효합니다. |
| , | 문화권별 그룹 구분 기호입니다. 에 플래그가 포함된 경우 현재 문화권의 그룹 구분 기호가 에 나타날 수 s style NumberStyles.AllowThousands 있습니다. |
| . | 문화권별 소수점 기호입니다. 에 플래그가 포함된 경우 현재 문화권의 소수점 기호가 에 나타날 수 s style NumberStyles.AllowDecimalPoint 있습니다. 구문 분석 작업이 성공하려면 숫자 0만 소수 자릿수로 표시할 수 있습니다. fractional_digits 다른 숫자가 포함되어 있으면 FormatException 이 throw됩니다. |
| E | 값이 지수(과학적) 표기법으로 표시되었음을 나타내는 "e" 또는 "E" 문자입니다. s에 플래그가 포함된 경우 매개 변수는 지수 표기법으로 숫자를 나타낼 수 style NumberStyles.AllowExponent 있습니다. |
| hexdigits | 0부터 f까지 또는 0부터 F까지의 16진수 시퀀스입니다. |
참고
의 종결 NUL(U+0000) s 문자는 인수 값에 관계없이 구문 분석 작업에서 style 무시됩니다.
숫자만 있는 NumberStyles.None 문자열(스타일에 해당)은 형식의 범위에 있는 경우 항상 성공적으로 구문 UInt16 분석됩니다. 나머지 멤버의 대부분은 NumberStyles 입력 문자열에 존재할 수 있지만 존재할 필요는 없는 요소를 제어합니다. 다음 표에서는 개별 NumberStyles 멤버가 에 있을 수 있는 요소에 미치는 영향을 s 나타냅니다.
NumberStyles 값 |
숫자 외에 에서 허용되는 요소 s |
|---|---|
| None | digits 요소만 해당합니다. |
| AllowDecimalPoint | 소수점(.) 및 소수 자릿수 요소입니다. |
| AllowExponent | 지수 표기법과 함께 exponential_digits 나타내는 "e" 또는 "E" 문자입니다. |
| AllowLeadingWhite | 의 시작 부분에 있는 ws s 요소입니다. |
| AllowTrailingWhite | 끝에 있는 ws s 요소입니다. |
| AllowLeadingSign | 의 시작 부분에 있는 부호 s 요소입니다. |
| AllowTrailingSign | 끝에 있는 부호 s 요소입니다. |
| AllowParentheses | 숫자 값을 묶는 괄호 형식의 부호 요소입니다. |
| AllowThousands | 그룹 구분 기호(,) 요소입니다. |
| AllowCurrencySymbol | 통화($) 요소입니다. |
| Currency | 모든 요소입니다. 그러나 는 s 16진수 또는 숫자를 지수 표기법으로 나타낼 수 없습니다. |
| Float | 의 시작 또는 끝에 있는 ws s 요소, 의 시작 부분에 있는 기호 s 및 소수점(.) 기호입니다. s매개 변수는 지수 표기법도 사용할 수 있습니다. |
| Number | ws, sign , 그룹 구분 기호(,) 및 소수점(.) 요소입니다. |
| Any | 모든 요소입니다. 그러나 는 s 16진수를 나타낼 수 없습니다. |
에서 특정 스타일 요소를 허용하지만 필요하지 않은 다른 값과 달리 스타일 값은 의 NumberStyles 개별 s 숫자 NumberStyles.AllowHexSpecifier 문자가 항상 s 16진수 문자로 해석된다는 것을 의미합니다. 유효한 16진수 문자는 0-9, A-F 및 a-f입니다. 접두사(예: "0x")는 지원되지 않으며 구문 분석 작업이 실패합니다. 매개 변수와 결합할 수 있는 다른 style 플래그는 NumberStyles.AllowLeadingWhite 및 NumberStyles.AllowTrailingWhite 뿐입니다. NumberStyles(열거형에는 공백 플래그를 모두 포함하는 복합 숫자 스타일 NumberStyles.HexNumber 가 포함됩니다.)
참고
가 s 16진수의 문자열 표현인 경우 16진수로 구분하는 장식(예: 또는 )이 앞에 올 수 0x &h 없습니다. 이렇게 하면 변환이 실패합니다.
s매개 변수는 NumberFormatInfo 현재 시스템 문화권에 대해 초기화되는 개체의 서식 지정 정보를 사용하여 구문 분석됩니다. 구문 분석 작업에 서식 정보가 사용되는 문화권 을 지정하려면 Parse(String, NumberStyles, IFormatProvider) 오버로드를 호출합니다.
추가 정보
적용 대상
Parse(String)
숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다.
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
매개 변수
- s
- String
변환할 숫자를 나타내는 문자열입니다.
반환
s에 있는 수에 해당하는 16비트 부호 없는 정수입니다.
- 특성
예외
s이(가) null인 경우
s가 올바른 형식이 아닙니다.
예제
다음 예제에서는 Parse(String) 메서드를 호출하여 문자열 배열의 각 요소를 부호 없는 16비트 정수로 변환합니다.
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
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
설명
s매개 변수는 다음 형식으로 된 숫자의 문자열 표현 이어야 합니다.
[ws] [sign] 숫자[ws]
대괄호 ([ 및 ]) 안의 요소는 선택적 요소입니다. 다음 표에서는 각 요소에 대해 설명합니다.
| 요소 | 설명 |
|---|---|
| ws | 선택적 공백입니다. |
| sign | 선택적 기호입니다. 유효한 부호 문자는 NumberFormatInfo.NegativeSign 현재 문화권의 및 속성에 의해 결정 됩니다 NumberFormatInfo.PositiveSign . 그러나 음수 부호 기호는 0 에서만 사용할 수 있습니다. 그렇지 않으면 메서드는을 throw OverflowException 합니다. |
| 숫자 | 0에서 9 사이의 숫자 시퀀스입니다. 선행 0은 무시됩니다. |
참고
매개 변수로 지정된 s 문자열은 스타일을 사용하여 해석됩니다. NumberStyles.Integer 그룹 구분 기호 또는 소수 구분 기호를 포함할 수 없으며 소수 부분을 가질 수 없습니다.
s매개 변수는 System.Globalization.NumberFormatInfo 현재 시스템 문화권에 대해 초기화되는 개체의 서식 지정 정보를 사용하여 구문 분석됩니다. 자세한 내용은 NumberFormatInfo.CurrentInfo를 참조하세요. 특정 문화권의 서식 지정 정보를 사용하여 문자열을 구문 분석하려면 Parse(String, IFormatProvider) 메서드를 사용합니다.
추가 정보
적용 대상
Parse(String, IFormatProvider)
지정된 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 16비트 부호 없는 정수로 변환합니다.
public:
static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider);
[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
매개 변수
- s
- String
변환할 숫자를 나타내는 문자열입니다.
- provider
- IFormatProvider
s에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.
반환
s에 지정된 수에 해당하는 16비트 부호 없는 정수입니다.
- 특성
예외
s이(가) null인 경우
s가 올바른 형식이 아닙니다.
예제
다음 예제에서는 두 개의 더하기 기호(++)를 양수 부호로 사용하는 사용자 지정 문화권의 인스턴스화합니다. 그런 다음 Parse(String, IFormatProvider) 메서드를 호출하여 이 사용자 지정 CultureInfo 문화권과 고정 문화권 모두를 나타내는 개체를 사용하여 문자열 배열을 구문 분석합니다.
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.
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.
설명
s매개 변수에는 다음과 같은 여러 형식이 포함됩니다.
[ws] [sign] digits[ws]
대괄호([ 및 ])의 항목은 선택 사항입니다. 다음 표에서는 각 요소에 대해 설명합니다.
| 요소 | Description |
|---|---|
| ws | 선택적 공백입니다. |
| sign | 선택적 부호이거나, 가 0 값을 나타내는 경우 s 음수 기호입니다. |
| 숫자 | 0에서 9까지의 숫자 시퀀스입니다. |
s 매개 변수는 스타일을 사용하여 NumberStyles.Integer 해석됩니다. 바이트 값의 10진수 외에도 선행 및 후행 공백과 선행 기호만 허용됩니다. 음수 기호가 있는 경우 는 s 0 값을 나타내야 합니다. 그렇지 않으면 메서드가 을 throw합니다. OverflowException 에 있을 수 있는 문화권별 서식 지정 정보와 함께 스타일 요소를 명시적으로 정의 하려면 s 사용 된 Parse(String, NumberStyles, IFormatProvider) 메서드.
provider매개 변수는 IFormatProvider GetFormat 메서드가 NumberFormatInfo 형식에 대한 문화권별 정보를 제공하는 개체를 반환하는 구현입니다. s 매개 변수를 사용하여 구문 분석 작업에 사용자 지정 서식 정보를 제공하는 세 가지 방법이 provider 있습니다.
서식 지정 정보를 제공하는 실제 개체를 전달할 수 NumberFormatInfo 있습니다. (의 GetFormat 구현은 단순히 자신을 반환합니다.)
CultureInfo형식을 사용할 문화권을 지정하는 개체를 전달할 수 있습니다. 해당 NumberFormat 속성은 서식 지정 정보를 제공합니다.
사용자 지정 구현을 전달할 수 IFormatProvider 있습니다. 해당 GetFormat 메서드는 형식 지정 정보를 제공하는 개체를 인스턴스화하고 반환해야 NumberFormatInfo 합니다.
하는 경우 provider 는 null , 현재 NumberFormatInfo 문화권에 대 한 사용 되는.