Boolean.TryParse 方法

定義

多載

TryParse(ReadOnlySpan<Char>, Boolean)

嘗試將代表邏輯值的指定範圍轉換為對等 Boolean

TryParse(String, Boolean)

嘗試將指定之邏輯值的字串表示轉換成對等的 Boolean

TryParse(ReadOnlySpan<Char>, Boolean)

嘗試將代表邏輯值的指定範圍轉換為對等 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

參數

value
ReadOnlySpan<Char>

範圍,其包含代表所要轉換值的字元。

result
Boolean

當這個方法傳回時,如果轉換成功,則會在 true 等於 value 時包含 TrueString,或在 false 等於 value 時包含 FalseString。 如果轉換失敗,則包含 false。 如果 valuenull,或者不等於 TrueStringFalseString 欄位的值,則轉換失敗。

傳回

Boolean

如果 value 轉換成功,則為 true,否則為 false

適用於

TryParse(String, Boolean)

嘗試將指定之邏輯值的字串表示轉換成對等的 Boolean

public:
 static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] bool % result);
public static bool TryParse (string value, 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

字串,包含要轉換的值。

result
Boolean

當這個方法傳回時,如果轉換成功,則會在 true 等於 value 時包含 TrueString,或在 false 等於 value 時包含 FalseString。 如果轉換失敗,則包含 false。 如果 valuenull,或者不等於 TrueStringFalseString 欄位的值,則轉換失敗。

傳回

Boolean

如果 value 轉換成功,則為 true,否則為 false

範例

下列範例會呼叫 TryParse 方法來剖析字串的陣列。 請注意,只有在要剖析的字串為 "True" (TrueString 欄位) 的值或 "False" (欄位值 FalseString) 不區分大小寫的比較時,剖析作業才會成功。

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'.

備註

TryParse方法與 Parse 方法類似,不同之處在于 TryParse 如果轉換失敗,方法不會擲回例外狀況。

value參數的前面或後面可以是空白字元。 比較是序數和不區分大小寫。

另請參閱

適用於