Convert String to Boolean in Swift



This tutorial will discuss how to write swift program to convert string type variable to boolean.

Swift support various datatypes and string and bool are one of them. String is an ordered collection of characters. For example: "TutorialsPoint", "Pinky", etc. To create a string type variable we use String keyword. Whereas Boolean represent bool values. Bool has only two values either true or false. And to create boolean type variable we use Bool keyword.

Swift does not provide any dedicated constructor (like String(), Int())to convert String into Bool. So we can convert boolean to string using any of the following approach.

Below is a demonstration of the same ?

Input

Suppose our given input is ?

Value = "true"

Output

The desired output would be ?

Bool = true

Method 1

In this approach, we convert string into boolean using comparison operator(==).

Example

The following program shows how to convert string type variable to boolean.

import Foundation
import Glibc

// String type variable
var myStr : String = "true"

// Bool type variable 
var myValue : Bool

// Convert string into bool
myValue = (myStr == "true")

print("Boolean value:", myValue)
print("Type of myValue variable:", type(of: myValue))

Output

Boolean value: true
Type of myValue variable: Bool

Here, in the above code, we have a string = "true". Now we convert string into bool by comparing entered string with "true" using (==) operator. If Both the strings are equal then it return true. Otherwise return false. Here both the strings are equal so it return true and the result assign to the bool variable.

We can use this approach but it is not the most efficient approach to covert string into bool.

Method 2

As we know that Bool data type can accept only two values either true or false. But a user can have multiple string representation for boolean values like "TRUE", "FALSE", "0", "1", "T", "F", "YES", "NO", "yes", "no", "True", "False", etc. So solve this problem we use switch case statement to convert string into boolean.

Example

The following program shows how to convert string type variable to boolean.

import Foundation
import Glibc

// String type variable
var myStr : String = "FALSE"

// Bool type variable 
var myValue : Bool 

// Convert String into Bool
// Using switch case statement
switch myStr.lowercased(){
   case "true", "t", "1", "yes", "y":
   myValue = true
   print("Boolean Value:", myValue)
   case "false", "f", "no", "0", "n":
   myValue = false
   print("Boolean Value:", myValue)
   default:
   print("Entered value is not valid boolean value")
}

Output

Boolean Value: false

Here, in the above code, we have string = "FALSE". Now we convert string into bool using switch case statement. In the switch expression, we convert the given string into lower case using lowercased() function. Now we match the pattern with each case and the match found in the second case. So the code present in the second case is executed ?

myValue = false
print("Boolean Value:", myValue)

So this is how we convert string into boolean using switch-case statement.

Method 3

A user can have multiple string to represent boolean values like "TRUE", "FALSE", "0", "1", "T", "F", "YES", "NO", "yes", "no", "True", "False", etc. But Bool data type can accept only two values either true or false. So, we use array to convert string into boolean.

Example

The following program shows how to convert string type variable to boolean.

import Foundation
import Glibc

// String type variable
var myStr : String = "TRUE"

// Bool type variable 
var myValue : Bool 

// Create array
var forTrue = ["true", "t", "1", "yes", "y"]
var forFalse = ["false", "f", "no", "0", "n"]

var temp = myStr.lowercased()

// Convert String into Bool
if forTrue.contains(temp){
   myValue = true
   print("Boolean Value:", myValue)
}
else if forFalse.contains(temp){
   myValue = false
   print("Boolean Value:", myValue)
}
else{
   print("Entered value is not valid boolean value")
}

Output

Boolean Value: true

Here, in the above code, we have string = "TRUE". Now we create two arrays for all the possible representation of true and false ?

var forTrue = ["true", "t", "1", "yes", "y"]
var forFalse = ["false", "f", "no", "0", "n"]

After that we convert the given string into lowercase. Now we use contains() function to check the given string is present in the array or not ?

if forTrue.contains(temp){
   myValue = true
   print("Boolean Value:", myValue)
}
else if forFalse.contains(temp){
   myValue = false
   print("Boolean Value:", myValue)
}
else{
   print("Entered value is not valid boolean value")
}

Here we have String = "true" which is present in the forTrue array so we assign true to myValue variable(it is of bool type) and print the output on the screen. If the entered value is other than the given values then we get "Entered value is not valid boolean value" as an output.

Updated on: 2022-12-13T14:41:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements