
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift program to convert string type variable into boolean
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.
- Related Articles
- Swift program to convert boolean variable into string
- Swift Program to convert int type variable to string
- Swift program to convert double type variable to string
- Golang Program to convert string type variables into Boolean
- C++ Program to Convert String Type Variables into Boolean
- Swift Program to convert int type variable to double
- C++ Program to convert Boolean Variables into String
- Golang Program to convert boolean variables into string
- Haskell Program to convert boolean variables into string
- Golang Program to convert string type variables into int
- C++ Program to Convert int Type Variables into String
- Java Program to convert String to Boolean
- Java Program to convert Boolean to String
- Golang Program to convert Boolean to String
- Haskell Program to Convert Boolean to String
