Boolean.TryParse Boolean.TryParse Boolean.TryParse Boolean.TryParse Method

定義

多載

TryParse(ReadOnlySpan<Char>, Boolean) TryParse(ReadOnlySpan<Char>, Boolean) TryParse(ReadOnlySpan<Char>, Boolean) TryParse(ReadOnlySpan<Char>, Boolean)
TryParse(String, Boolean) TryParse(String, Boolean) TryParse(String, Boolean) TryParse(String, Boolean)

嘗試將指定之邏輯值的字串表示轉換成對等的 BooleanTries to convert the specified string representation of a logical value to its Boolean equivalent. 傳回值會指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, Boolean) TryParse(ReadOnlySpan<Char>, Boolean) TryParse(ReadOnlySpan<Char>, Boolean) TryParse(ReadOnlySpan<Char>, Boolean)

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

參數

傳回

TryParse(String, Boolean) TryParse(String, Boolean) TryParse(String, Boolean) TryParse(String, Boolean)

嘗試將指定之邏輯值的字串表示轉換成對等的 BooleanTries to convert the specified string representation of a logical value to its Boolean equivalent. 傳回值會指出轉換成功或失敗。A return value indicates whether the conversion succeeded or failed.

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

參數

value
String String String String

字串,包含要轉換的值。A string containing the value to convert.

result
Boolean Boolean Boolean Boolean

當這個方法傳回時,如果轉換成功,則會在 true 等於 value 時包含 TrueString,或在 false 等於 value 時包含 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.

傳回

如果 true 轉換成功,則為 value,否則為 falsetrue if value was converted successfully; otherwise, false.

範例

下列範例會呼叫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'.

備註

方法與Parse方法類似,不同之處在于TryParse如果轉換失敗,方法不會擲回例外狀況。 TryParseThe 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.

另請參閱

適用於