How to perform string comparison in TypeScript?


In this TypeScript tutorial, users will learn to make string comparisons. The string comparison is the basic operation required while working with any programming language.

Suppose we are developing a web or Android application and need to compare string data such as user id, user name, password, etc. In such cases, string comparison becomes very useful.

Using the strict equality (===) operator

The best way to compare the strings in TypeScript is to use the strict equality operator. The strict equality operator first checks the types of the left and right operands, and if it matches, it compares the value of the string and returns the Boolean value based on the match of the string.

Users can follow the syntax below to compare the strings using the strict equality operator in TypeScript.

Syntax

let string1: string = " ";
let string2: string = " ";
let value: boolean = string1 === string2;

In the above syntax, we have created the two strings and compared them using the strict equality operator.

Steps

STEP 1 − Create two different strings - 'string1', and 'string2'.

STEP 2 − Assign the comparison result of 'string1' and 'string2' into the 'value1' Boolean variable.

STEP 3 − As a final step, we print the value of 'value1'. If the value of 'value1' is 'true' then the strings are 'equal' else, they are 'not equal'.

Example 1

In the example below, we compare two strings - string1 and string2. We have taken both strings as the same, so we expect the output to be 'true'.

// Creating 2 string variables and initializing with values
let string1: string = "TutorialsPoint";
let string2: string = "TutorialsPoint";

// Comparing the strings.
let value1: boolean = string1 === string2;

// Show result
console.log(value1);

On compiling, it will generate the following JavaScript code −

// Creating 2 string variables and initializing with values
var string1 = "TutorialsPoint";
var string2 = "TutorialsPoint";
// Comparing the strings.
var value1 = string1 === string2;
// Show the result
console.log(value1);

The above code will produce the following output −

Output

true

Example 2

In the example below, we compare two strings- string1 and string2. We have taken both strings as different, so we expect the output to be 'false'.

// Creating 2 string variables and initializing with values
let string1: string = "TutorialsPoint";
let string2: string = "Tutorial";

// Comparing the strings.
let value1: boolean = string1 === string2;

// Show result
console.log(value1);

On compiling, it will generate the following JavaScript code −

// Creating 2 string variables and initializing with values
var string1 = "TutorialsPoint";
var string2 = "Tutorial";
// Comparing the strings.
var value1 = string1 === string2;
// Show the result
console.log(value1);

The above code will produce the following output −

Output

false

Using the strict equality operator to do case-insensitive string comparison

Sometimes, we require to compare the string case-insensitively. The case-insensitive string comparison means all characters of both strings are the same but not the case for all characters of strings.

Users can follow the syntax below to compare the strings case-insensitively.

Syntax

let string1: string = "TUTORIALSPOINT";
let string2: string = "tutorialspoint";
// Comparing the strings.
let value1: boolean = string1.toLowerCase() === string2.toLowerCase();
let value2: boolean = string1.toUpperCase() === string2.toUpperCase();

First, we have converted the strings to lowercase in the above syntax and compared them. Also, we have converted the strings in uppercase and compared them.

Steps

STEP 1 − Create two strings - 'str1' and 'str2', containing the same value but with different cases. 

STEP 2 − Next, convert the 'str1' and 'str2' to lowercase, or uppercase.

STEP 3 − Compare them using the strict equality operator, and stored its result in the `val1` Boolean variable.

STEP 4 − In the last step, we are also printing the value of the `val1`.

Example

To compare the strings case-insensitively, users can simply convert both strings into lowercase or uppercase and compare them. In the example below, we compare two strings containing the same value but with different cases.

//  Creating 2 string variables and initializing with values
let str1: string = "TUTORIALSPOINT";
let str2: string = "tutorialspoint";

// Comparing the strings.
let val1: boolean = str1.toLowerCase() === str2.toLowerCase();
let val2: boolean = str1.toUpperCase() === str2.toUpperCase();

// Show the comparison results
console.log("After converting both strings into the lowercase, comparison result is : " + val1);
console.log("After converting both strings into the lowercase, comparison result is : " + val2);

On compiling, it will generate the following JavaScript code −

//  Creating 2 string variables and initializing with values
var str1 = "TUTORIALSPOINT";
var str2 = "tutorialspoint";
// Comparing the strings.
var val1 = str1.toLowerCase() === str2.toLowerCase();
var val2 = str1.toUpperCase() === str2.toUpperCase();
// Show the comparison results
console.log("After converting both strings into lowercase, comparison result is : " + val1);
console.log("After converting both strings into uppercase, comparison result is : " + val2);

The above code will produce the following output −

Output

After converting both strings into lowercase, comparison result is : true
After converting both strings into uppercase, comparison result is : true

In the above output, users can see that value of the 'val1' and 'val2' is true as we are comparing both strings after converting into the same case, either lowercase or uppercase.

Using the loose equality (==) operator

Also, users can use the loose equality operator (==) to compare the strings. It will also give the same result as the strict equality operator gives, and it is the main benefit of TypeScript over JavaScript. Users can follow the Example below to compare the strings using the loose equality operator.

Example

In the below Example, users can see that we are comparing the 'str1' and 'str2' using the loose equality operator.

let str1: string = "Hello";
let str2: string = "World!";
// Comparing the strings.
let val1: boolean = str1 == str2;
console.log(val1);

On compiling, it will generate the following JavaScript code −

var str1 = "Hello";
var str2 = "World!";
// Comparing the strings.
var val1 = str1 == str2;
console.log(val1);

The above code will produce the following output −

Output

false

Users learned to compare the strings in this tutorial. Unlike JavaScript, the strict and lose quality operator works the same for the string comparison as we define the data type of variables in TypeScript, and it doesn't allow us to compare the variables of different data types.

Updated on: 10-Sep-2023

34K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements