Int16.TryParse 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
숫자의 문자열 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.
오버로드
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16) |
UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다. |
TryParse(ReadOnlySpan<Char>, Int16) |
지정된 스타일 및 문화권별 형식으로 된 숫자의 범위 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다. |
TryParse(String, Int16) |
숫자의 문자열 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다. |
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16) |
문자 범위를 값으로 구문 분석하려고 시도합니다. |
TryParse(String, IFormatProvider, Int16) |
문자열을 값으로 구문 분석하려고 시도합니다. |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int16) |
UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다. |
TryParse(ReadOnlySpan<Byte>, Int16) |
숫자의 문자열 표현을 포함하는 UTF-8 문자 범위를 해당하는 16비트 부호 있는 정수로 변환하려고 시도합니다. |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16) |
지정된 스타일 및 문화권별 형식으로 된 숫자의 범위 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다. |
TryParse(String, NumberStyles, IFormatProvider, Int16) |
지정된 스타일 및 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다. |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다.
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = IUtf8SpanParsable<short>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out short result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * int16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Short) As Boolean
매개 변수
- utf8Text
- ReadOnlySpan<Byte>
구문 분석할 UTF-8 문자의 범위입니다.
- provider
- IFormatProvider
utf8Text
에 대한 문화권별 서식 정보를 제공하는 개체입니다.
- result
- Int16
반환 시 에는 성공적으로 구문 분석 utf8Text
한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.
반환
true
성공적으로 구문 분석되었으면 utf8Text
이고, false
그렇지 않으면 입니다.
적용 대상
TryParse(ReadOnlySpan<Char>, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
지정된 스타일 및 문화권별 형식으로 된 숫자의 범위 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<char> s, out short result);
static member TryParse : ReadOnlySpan<char> * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Short) As Boolean
매개 변수
- s
- ReadOnlySpan<Char>
변환할 숫자를 나타내는 문자를 포함하는 범위입니다.
- result
- Int16
이 메서드는 변환이 성공한 경우 s
에 포함된 숫자에 해당하는 16비트 부호 있는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 매개 변수가 s
null
또는 Empty가 와 호환style
되는 형식이 아니거나 Int16.MinValue보다 작거나 Int16.MaxValue보다 큰 숫자를 나타내는 경우 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result
에 제공된 모든 값을 덮어쓰게 됩니다.
반환
s
이(가) 성공적으로 변환되었으면 true
이고, 그렇지 않으면 false
입니다.
적용 대상
TryParse(String, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
숫자의 문자열 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (string s, out short result);
public static bool TryParse (string? s, out short result);
static member TryParse : string * int16 -> bool
Public Shared Function TryParse (s As String, ByRef result As Short) As Boolean
매개 변수
- s
- String
변환할 숫자가 포함된 문자열입니다.
- result
- Int16
이 메서드는 변환이 성공한 경우 s
에 포함된 숫자에 해당하는 16비트 부호 있는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 매개 변수가 s
또는 Empty이 null
올바른 형식이 아니거나 Int16.MinValue보다 작거나 Int16.MaxValue보다 큰 숫자를 나타내는 경우 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result
에 제공된 모든 값을 덮어쓰게 됩니다.
반환
s
이(가) 성공적으로 변환되었으면 true
이고, 그렇지 않으면 false
입니다.
예제
다음 예제에서는 여러 문자열 값이 있는 메서드를 호출 Int16.TryParse(String, Int16) 합니다.
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("16051");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
short number;
bool result = Int16.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
open System
let tryToParse (value: string) =
match Int16.TryParse value with
| true, number -> printfn "Converted '{value}' to {number}."
| _ ->
let value = if isNull value then "" else value
printfn $"Attempted conversion of '{value}' failed."
tryToParse null
tryToParse "16051"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse " -322 "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
Module StringParsing
Public Sub Main()
TryToParse(Nothing)
TryToParse("16051")
TryToParse("9432.0")
TryToParse("16,667")
TryToParse(" -322 ")
TryToParse("+4302")
TryToParse("(100)")
TryToParse("01FA")
End Sub
Private Sub TryToParse(value As String)
Dim number As Int16
Dim result As Boolean = Int16.TryParse(value, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
If value Is Nothing Then value = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", value)
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '' failed.
' Converted '16051' to 16051.
' Attempted conversion of '9432.0' failed.
' Attempted conversion of '16,667' failed.
' Converted ' -322 ' to -322.
' Converted '+4302' to 4302.
' Attempted conversion of '(100)' failed.
' Attempted conversion of '01FA' failed.
이 예제에서 메서드가 TryParse(String, Int16) 변환할 수 없는 문자열 중 일부는 다음과 같습니다.
"9432.0". 문자열에 10진수 구분 기호를 포함할 수 없으므로 변환이 실패합니다. 정수 자릿수만 포함해야 합니다.
"16,667". 문자열에 그룹 구분 기호를 포함할 수 없으므로 변환이 실패합니다. 정수 자릿수만 포함해야 합니다.
"(100)". 문자열에 현재 문화권 NumberFormatInfo.NegativeSign 및 NumberFormatInfo.NumberNegativePattern 속성으로 정의된 기호 이외의 음수 기호를 포함할 수 없으므로 변환이 실패합니다.
"01FA". 문자열에 16진수 숫자를 포함할 수 없으므로 변환이 실패합니다. 소수 자릿수만 포함해야 합니다.
설명
메서드는 Int16.TryParse(String, Int16) 구문 분석된 값을 반환하는 대신 구문 분석 작업이 성공했는지 여부를 나타내는 부울 값을 반환하여 메서드와 Int16 다릅니다Int16.Parse(String). 예외 처리를 사용하여 유효하지 않고 성공적으로 구문 분석할 수 없는 경우 s
를 테스트 FormatException 할 필요가 없습니다.
매개 변수는 s
양식에 있는 숫자의 문자열 표현이어야 합니다.
[ws] [sign]digits[ws]
대괄호([ 및 ])의 항목은 선택 사항입니다. 다음 표에서는 각 요소에 대해 설명합니다.
요소 | 설명 |
---|---|
ws | 선택적 공백입니다. |
sign | 선택적 기호입니다. |
숫자 | 0에서 9까지의 숫자 시퀀스입니다. |
매개 변수는 s
스타일을 사용하여 해석됩니다 NumberStyles.Integer . 소수 자릿수 외에도 선행 기호와 함께 선행 및 후행 공백만 허용됩니다. 에 있을 s
수 있는 문화권별 서식 정보와 함께 스타일 요소를 명시적으로 정의하려면 메서드를 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 사용합니다.
s
매개 변수는 현재 시스템 문화권에 대해 초기화된 개체의 NumberFormatInfo 서식 정보를 사용하여 구문 분석됩니다. 자세한 내용은 CurrentInfo를 참조하세요.
메서드의 이 오버로드는 매개 변수의 TryParse 모든 숫자를 s
10진수로 해석합니다. 16진수의 문자열 표현을 구문 분석하려면 오버로드를 호출합니다 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) .
추가 정보
- Parse(String)
- ToString()
- .NET에서 숫자 문자열 구문 분석
- 샘플: .NET Core WinForms 서식 유틸리티(C#)
- 샘플: .NET Core WinForms 서식 유틸리티(Visual Basic)
적용 대상
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
문자 범위를 값으로 구문 분석하려고 시도합니다.
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = ISpanParsable<short>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out short result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Short) As Boolean
매개 변수
- s
- ReadOnlySpan<Char>
구문 분석할 문자의 범위입니다.
- provider
- IFormatProvider
s
에 대한 문화권별 서식 정보를 제공하는 개체입니다.
- result
- Int16
이 메서드가 반환될 때 에는 성공적으로 구문 분석 s
한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.
반환
true
성공적으로 구문 분석되었으면 s
이고, false
그렇지 않으면 입니다.
적용 대상
TryParse(String, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
문자열을 값으로 구문 분석하려고 시도합니다.
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = IParsable<short>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out short result);
static member TryParse : string * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Short) As Boolean
매개 변수
- s
- String
구문 분석할 문자열입니다.
- provider
- IFormatProvider
s
에 대한 문화권별 서식 정보를 제공하는 개체입니다.
- result
- Int16
이 메서드가 반환될 때 에는 성공적으로 구문 분석 s
한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.
반환
true
성공적으로 구문 분석되었으면 s
이고, false
그렇지 않으면 입니다.
적용 대상
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
UTF-8 문자의 범위를 값으로 구문 분석하려고 시도합니다.
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = System::Numerics::INumberBase<short>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
매개 변수
- utf8Text
- ReadOnlySpan<Byte>
구문 분석할 UTF-8 문자의 범위입니다.
- style
- NumberStyles
에 utf8Text
있을 수 있는 숫자 스타일의 비트 조합입니다.
- provider
- IFormatProvider
utf8Text
에 대한 문화권별 서식 정보를 제공하는 개체입니다.
- result
- Int16
반환 시 에는 성공적으로 구문 분석 utf8Text
한 결과 또는 실패 시 정의되지 않은 값이 포함됩니다.
반환
true
성공적으로 구문 분석되었으면 utf8Text
이고, false
그렇지 않으면 입니다.
적용 대상
TryParse(ReadOnlySpan<Byte>, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
숫자의 문자열 표현을 포함하는 UTF-8 문자 범위를 해당하는 16비트 부호 있는 정수로 변환하려고 시도합니다.
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out short result);
static member TryParse : ReadOnlySpan<byte> * int16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Short) As Boolean
매개 변수
- utf8Text
- ReadOnlySpan<Byte>
변환할 숫자를 나타내는 UTF-8 문자를 포함하는 범위입니다.
- result
- Int16
이 메서드가 반환될 때 변환에 성공한 경우 에 포함된 utf8Text
수에 해당하는 부호 있는 16비트 정수 값이 포함되거나 변환에 실패한 경우 0이 포함됩니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 결과에 제공된 모든 값을 덮어쓰게 됩니다.
반환
utf8Text
이(가) 성공적으로 변환되었으면 true
이고, 그렇지 않으면 false
입니다.
적용 대상
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
지정된 스타일 및 문화권별 형식으로 된 숫자의 범위 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = System::Numerics::INumberBase<short>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
매개 변수
- s
- ReadOnlySpan<Char>
변환할 숫자를 나타내는 문자를 포함하는 범위입니다. style
에서 지정된 스타일을 사용하여 해석하는 범위입니다.
- style
- NumberStyles
s
에 나타날 수 있는 스타일 요소를 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.
- provider
- IFormatProvider
s
에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.
- result
- Int16
이 메서드는 변환이 성공한 경우 s
에 포함된 숫자에 해당하는 16비트 부호 있는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 매개 변수가 s
null
또는 Empty가 와 호환style
되는 형식이 아니거나 Int16.MinValue보다 작거나 Int16.MaxValue보다 큰 숫자를 나타내는 경우 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result
에 제공된 모든 값을 덮어쓰게 됩니다.
반환
s
이(가) 성공적으로 변환되었으면 true
이고, 그렇지 않으면 false
입니다.
적용 대상
TryParse(String, NumberStyles, IFormatProvider, Int16)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
지정된 스타일 및 문화권별 형식으로 된 숫자의 문자열 표현을 해당하는 16비트 부호 있는 정수로 변환합니다. 반환 값은 변환이 성공했는지 아니면 실패했는지를 나타냅니다.
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result) = System::Numerics::INumberBase<short>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
매개 변수
- s
- String
변환할 숫자가 포함된 문자열입니다. 이 문자열은 style
이 지정하는 스타일을 사용하여 해석됩니다.
- style
- NumberStyles
s
에 나타날 수 있는 스타일 요소를 나타내는 열거형 값의 비트 조합입니다. 지정할 일반적인 값은 Integer입니다.
- provider
- IFormatProvider
s
에 대한 문화권별 형식 지정 정보를 제공하는 개체입니다.
- result
- Int16
이 메서드는 변환이 성공한 경우 s
에 포함된 숫자에 해당하는 16비트 부호 있는 정수 값을 반환하고, 변환이 실패한 경우 0을 반환합니다. 매개 변수가 s
null
또는 Empty가 와 호환style
되는 형식이 아니거나 Int16.MinValue보다 작거나 Int16.MaxValue보다 큰 숫자를 나타내는 경우 변환이 실패합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다. 원래 result
에 제공된 모든 값을 덮어쓰게 됩니다.
반환
s
이(가) 성공적으로 변환되었으면 true
이고, 그렇지 않으면 false
입니다.
예외
예제
다음 예제에서는 여러 문자열 값이 있는 메서드를 호출 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 합니다.
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "10677";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
short number;
bool result = Int16.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
else
Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
}
}
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match Int16.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let numericString = "10677"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-30677"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "10345.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "10345.72";
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "22,593"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse numericString styles
let numericString = "12E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "12E03"
callTryParse numericString styles
let numericString = "80c1"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x80C1"
callTryParse numericString NumberStyles.HexNumber
0
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "10677"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-30677"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "10345.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "22,593"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(numericString, styles)
numericString = "12E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E03"
CallTryParse(numericString, styles)
numericString = "80c1"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x80C1"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As Short
Dim result As Boolean = Int16.TryParse(stringToConvert, styles, _
CultureInfo.InvariantCulture, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
Convert.ToString(stringToConvert))
End If
End Sub
End Module
' The example displays the following output to the console:
' Converted '10677' to 10677.
' Attempted conversion of '-30677' failed.
' Converted '10345.00' to 10345.
' Attempted conversion of '10345.72' failed.
' Converted '22,593' to 22593.
' Attempted conversion of '12E-01' failed.
' Converted '12E03' to 12000.
' Converted '80c1' to -32575.
' Attempted conversion of '0x80C1' failed.
설명
메서드는 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 구문 분석된 값을 반환하는 대신 구문 분석 작업이 성공했는지 여부를 나타내는 부울 값을 반환하여 메서드와 Int16 다릅니다Int16.Parse(String, NumberStyles, IFormatProvider). 예외 처리를 사용하여 유효하지 않고 성공적으로 구문 분석할 수 없는 경우 s
를 테스트 FormatException 할 필요가 없습니다.
매개 변수는 style
구문 분석 작업이 성공하기 위해 매개 변수에 s
허용되는 스타일 요소(예: 공백 또는 양수 또는 음수 기호)를 정의합니다. 열거형의 비트 플래그 NumberStyles 조합이어야 합니다. 스타일 값에 따라 매개 변수에는 s
다음 요소가 포함될 수 있습니다.
[ws] [$] [sign] [digits,]digits[.fractional_digits][e[sign]digits][ws]
또는 매개 변수에 가 포함된 경우 입니다 style
NumberStyles.AllowHexSpecifier.
[ws]hexdigits[ws]
대괄호([ 및 ])의 항목은 선택 사항입니다. 다음 표에서는 각 요소에 대해 설명합니다.
요소 | 설명 |
---|---|
ws | 선택적 공백입니다. 플래그를 포함하는 경우 style 의 s 시작 부분에 공백이 NumberStyles.AllowLeadingWhite 표시되거나, 의 끝에 s 플래그가 포함되면 style 공백이 NumberStyles.AllowTrailingWhite 나타날 수 있습니다. |
$ | 문화권별 통화 기호입니다. 문자열의 위치는 매개 변수의 메서드 provider 에서 반환된 NumberFormatInfo 개체의 속성에 의해 CurrencyPositivePatternGetFormat 정의됩니다. 플래그가 포함된 경우 style 통화 기호가 NumberStyles.AllowCurrencySymbol 에 s 나타날 수 있습니다. |
sign | 선택적 기호입니다. |
숫자 | 0에서 9까지의 숫자 시퀀스입니다. |
, | 문화권별 천 단위 구분 기호입니다. 에 지정된 provider 문화권의 천 단위 구분 기호가 플래그를 포함하는 NumberStyles.AllowThousands 경우 style 에 s 나타날 수 있습니다. |
. | 문화권별 소수점 기호입니다. 에 지정된 provider 문화권의 소수점 기호는 플래그를 포함하는 NumberStyles.AllowDecimalPoint 경우 style 에 s 나타날 수 있습니다. |
fractional_digits | 숫자 0이 하나 이상 발생합니다. 소수 자릿수는 플래그가 포함된 NumberStyles.AllowDecimalPoint 경우에만 style 에 s 나타날 수 있습니다. |
e | 값이 지수 표기법으로 표시됨을 나타내는 'e' 또는 'E' 문자입니다. 매개 변수는 s 플래그를 포함하는 경우 style 지수 표기법으로 NumberStyles.AllowExponent 숫자를 나타낼 수 있습니다. |
hexdigits | 0에서 f까지 또는 0부터 F까지의 16진수 숫자 시퀀스입니다. |
참고
의 종결 NUL(U+0000) 문자 s
는 인수 값 style
에 관계없이 구문 분석 작업에서 무시됩니다.
10진수만 있는 문자열(플래그에 NumberStyles.None 해당)은 항상 성공적으로 구문 분석됩니다. 나머지 NumberStyles 멤버의 대부분은 이 입력 문자열에 존재할 필요는 없지만 존재할 필요는 없는 요소를 제어합니다. 다음 표에서는 개별 NumberStyles 멤버가 에 s
있을 수 있는 요소에 미치는 영향을 나타냅니다.
비 복합 NumberStyles 값 | 숫자 외에 에서 허용되는 요소 |
---|---|
NumberStyles.None | 10진수에만 해당합니다. |
NumberStyles.AllowDecimalPoint | 및fractional_digits 요소입니다. 그러나 fractional_digits 하나 이상의 숫자로만 구성되어야 합니다. 그렇지 않으면 메서드가 를 반환 false 합니다. |
NumberStyles.AllowExponent | 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. 지수 표기법의 숫자를 나타내는 경우 s 0이 아닌 소수 구성 요소가 없는 데이터 형식 범위 내의 Int16 정수를 나타내야 합니다. |
NumberStyles.AllowLeadingWhite | 의 시작 부분에 있는 ws 요소 입니다. |
NumberStyles.AllowTrailingWhite | 의 끝에 있는 ws 요소 입니다. |
NumberStyles.AllowLeadingSign | 숫자 앞에 기호가 나타날 수 있습니다. |
NumberStyles.AllowTrailingSign | 숫자 다음에 기호가 나타날 수 있습니다. |
NumberStyles.AllowParentheses | 숫자 값을 묶는 괄호 형식의 sign 요소입니다. |
NumberStyles.AllowThousands | , 요소입니다. |
NumberStyles.AllowCurrencySymbol | $ 요소입니다. |
NumberStyles.Currency | 모든 요소. s 매개 변수는 16진수 또는 지수 표기법의 숫자를 나타낼 수 없습니다. |
NumberStyles.Float | 의 시작 또는 끝에 s 있는 ws 요소 및 의 시작 부분에 s 기호및 기호입니다. 매개 변수는 s 지수 표기법을 사용할 수도 있습니다. |
NumberStyles.Number | ws, sign, thousands 구분 기호(,) 및 소수점(.) 요소입니다. |
NumberStyles.Any | 를 제외한 s 모든 스타일은 16진수를 나타낼 수 없습니다. |
플래그를 NumberStyles.AllowHexSpecifier 사용하는 s
경우 접두사 없이 16진수 값이어야 합니다. 예를 들어 "9AF3"은 성공적으로 구문 분석되지만 "0x9AF3"은 구문 분석하지 않습니다. 에 있을 style
수 있는 유일한 다른 플래그는 및 NumberStyles.AllowTrailingWhite입니다NumberStyles.AllowLeadingWhite. 열거형에는 NumberStyles 공백 플래그가 모두 포함된 복합 스타일 NumberStyles.HexNumber이 있습니다.
provider
매개 변수는 IFormatProvider 개체 또는 개체와 같은 구현이며 CultureInfoNumberFormatInfo 메서드는 GetFormat 개체를 NumberFormatInfo 반환합니다. 개체는 NumberFormatInfo 형식 s
에 대한 문화권별 정보를 제공합니다. 가 이 null
NumberFormatInfo 면 provider
현재 문화권의 개체가 사용됩니다.
추가 정보
적용 대상
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기