![]() |
In Typescript, sometimes you receive the data as strings but need to work with boolean values or identify the boolean equivalent of it. There are several approaches to convert string to boolean in TypeScript which are as follows: Table of Content Using Conditional StatementIn this approach, we simply use a conditional statement to compare the input string with a boolean value (true/false). If the comparison returns true, that ensures the original string was `true`. In the case where the comparison does not return true, that means the original string was something other than `true`. Syntax:function stringToBoolean(str: string): boolean {
return str.toLowerCase() === 'true';
} Example: TypeScript program defines
Output true Using JSON.parse() MethodIn this approach, we make the use of `JSON.Parse()` method to parse the string into its actual boolean form. But before parsing we makes sure that the string is converted into lowercase. Syntax:function stringToBoolean(str: string): boolean {
return JSON.parse(str.toLowerCase());
} Example: This TypeScript program utilizes JSON parsing in the `stringToBoolean` function to convert a string to a boolean, demonstrated by transforming ‘False’ to `false` and displaying the result.
Output false Using Type AssertionIn this approach, we use type assertion to explicitly declare the string as boolean. In this case also, we first need to convert the string into its lowercase. Syntax:function stringToBoolean(str: string): boolean {
return str.toLowerCase() as unknown as boolean;
} Example: TypeScript program using the`stringToBoolean` function uses type assertion to convert a string to a boolean, demonstrated by transforming ‘true’ to `true` and displaying the result.
Output true Using Regular ExpressionsIn this approach, we employ regular expressions to match common boolean representations within the input string. By defining patterns for boolean values like “true”, “false”, “yes”, “no”, “1”, and “0”, we can accurately determine the boolean equivalent of the input string. Syntax:function stringToBoolean(str: string): boolean {
const truePattern: RegExp = /^(true|yes|1)$/i;
const falsePattern: RegExp = /^(false|no|0)$/i;
if (truePattern.test(str)) {
return true;
} else if (falsePattern.test(str)) {
return false;
} else {
throw new Error('Invalid boolean string');
}
} Example: The following example demonstrates how to use regular expressions to convert a string to a boolean in TypeScript.
Output: true
false Using a Map ObjectIn this approach, we use a Map object to store key-value pairs of string representations and their corresponding boolean values. This method provides a clean and efficient way to convert a string to a boolean by leveraging the Map object’s get method. Syntax:function stringToBoolean(str: string): boolean {
const boolMap = new Map<string, boolean>([
['true', true],
['false', false],
['yes', true],
['no', false],
['1', true],
['0', false]
]);
const result = boolMap.get(str.toLowerCase());
if (result !== undefined) {
return result;
} else {
throw new Error('Invalid boolean string');
}
} Example: This TypeScript program uses a Map object to convert a string to its boolean value, illustrated by transforming various strings and displaying the results.
Output: true
false
true
false
Invalid boolean string |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |