Boolean.TryParse Method

Definition

Overloads

TryParse(ReadOnlySpan<Char>, Boolean)

尝试将逻辑值的指定范围表示形式转换为它的等效 BooleanTries to convert the specified span representation of a logical value to its Boolean equivalent.

TryParse(String, Boolean)

尝试将逻辑值的指定字符串表示形式转换为其等效的 Boolean 值。Tries to convert the specified string representation of a logical value to its Boolean equivalent.

TryParse(ReadOnlySpan<Char>, Boolean)

尝试将逻辑值的指定范围表示形式转换为它的等效 BooleanTries to convert the specified span representation of a logical value to its Boolean equivalent.

public:
 static bool TryParse(ReadOnlySpan<char> value, [Runtime::InteropServices::Out] bool % result);
public static bool TryParse (ReadOnlySpan<char> value, out bool result);
static member TryParse : ReadOnlySpan<char> * bool -> bool
Public Shared Function TryParse (value As ReadOnlySpan(Of Char), ByRef result As Boolean) As Boolean

Parameters

value
ReadOnlySpan<Char>

一个范围,包含表示要转换的值的字符。A span containing the characters representing the value to convert.

result
Boolean

此方法返回时,如果转换成功,若 truevalue 相等,则包含 TrueString,若 falsevalue 相等,则包含 FalseStringWhen this method returns, if the conversion succeeded, contains true if value is equal to TrueString or false if value is equal to FalseString. 如果转换失败,则包含 falseIf the conversion failed, contains false. 如果 valuenull 或不等于 TrueStringFalseString 字段的值,则转换失败。The conversion fails if value is null or is not equal to the value of either the TrueString or FalseString field.

Returns

Boolean

如果 true 成功转换,则为 value;否则为 falsetrue if value was converted successfully; otherwise, false.

TryParse(String, Boolean)

尝试将逻辑值的指定字符串表示形式转换为其等效的 Boolean 值。Tries to convert the specified string representation of a logical value to its Boolean equivalent.

public:
 static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] bool % result);
public static bool TryParse (string value, out bool result);
static member TryParse : string * bool -> bool
Public Shared Function TryParse (value As String, ByRef result As Boolean) As Boolean

Parameters

value
String

包含要转换的值的字符串。A string containing the value to convert.

result
Boolean

此方法返回时,如果转换成功,若 truevalue 相等,则包含 TrueString,若 falsevalue 相等,则包含 FalseStringWhen this method returns, if the conversion succeeded, contains true if value is equal to TrueString or false if value is equal to FalseString. 如果转换失败,则包含 falseIf the conversion failed, contains false. 如果 valuenull 或不等于 TrueStringFalseString 字段的值,则转换失败。The conversion fails if value is null or is not equal to the value of either the TrueString or FalseString field.

Returns

Boolean

如果 true 成功转换,则为 value;否则为 falsetrue if value was converted successfully; otherwise, false.

Examples

下面的示例调用 TryParse 方法来分析字符串数组。The following example calls the TryParse method to parse an array of strings. 请注意,仅当要分析的字符串为 "True" (TrueString 字段的值)或 "False" (FalseString 字段的值)时,分析操作才能成功,不区分大小写的比较。Note that the parse operation succeeds only if the string to be parsed is "True" (the value of the TrueString field) or "False" (the value of the FalseString field) in a case-insensitive comparison.

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { null, String.Empty, "True", "False",
                          "true", "false", "    true    ", "0",
                          "1", "-1", "string" };
      foreach (var value in values) {
         bool flag;
         if (Boolean.TryParse(value, out flag))
            Console.WriteLine("'{0}' --> {1}", value, flag);
         else
            Console.WriteLine("Unable to parse '{0}'.",
                              value == null ? "<null>" : value);
      }
   }
}
// The example displays the following output:
//       Unable to parse '<null>'.
//       Unable to parse ''.
//       'True' --> True
//       'False' --> False
//       'true' --> True
//       'false' --> False
//       '    true    ' --> True
//       Unable to parse '0'.
//       Unable to parse '1'.
//       Unable to parse '-1'.
//       Unable to parse 'string'.
Module Example
   Public Sub Main()
      Dim values() As String = { Nothing, String.Empty, "True", "False", 
                                 "true", "false", "    true    ", "0", 
                                 "1", "-1", "string" }
      For Each value In values
         Dim flag As Boolean
         
         If Boolean.TryParse(value, flag) Then
            Console.WriteLine("'{0}' --> {1}", value, flag)
         Else
            Console.WriteLine("Unable to parse '{0}'.", 
                              If(value Is Nothing, "<null>", value))
         End If         
      Next                                     
   End Sub
End Module
' The example displays the following output:
'       Unable to parse '<null>'.
'       Unable to parse ''.
'       'True' --> True
'       'False' --> False
'       'true' --> True
'       'false' --> False
'       '    true    ' --> True
'       Unable to parse '0'.
'       Unable to parse '1'.
'       Unable to parse '-1'.
'       Unable to parse 'string'.

Remarks

TryParse 方法类似于 Parse 方法,但如果转换失败,则 TryParse 方法不会引发异常。The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails.

value 参数的前面或后面可以是空格。The value parameter can be preceded or followed by white space. 比较是序号,不区分大小写。The comparison is ordinal and case-insensitive.

See also

Applies to