How to find the length of a string in TypeScript?


The string can contain a sequence of repeated and non-repeated characters. The length of the string means the total number of characters the string contains in the sequence of characters, including special characters, alphabetic characters, etc.

Here, we will learn different methods to calculate the length of the string in TypeScript.

Using the length property of the string

In TypeScript, the string is a library, or we can say it is a class. It contains some properties and methods, which we can call by taking the string class object as a reference. The length is also the property of the string class, which returns the total number of characters in the string.

Syntax

Users can follow the syntax below to use the string class's length property to find the string's length.

let string1: string = "TutorialsPoint";
let len = string1.length;

Example

In the example below, we have taken different kinds of strings and used the length property to find their length of them. The first string is a regular string without special characters, the second string contains special characters, and the third one contains spaces.

Users can observe in the output that the length property counts the spaces as a string character and returns the string length accordingly.

//  Regular string
let string1: string = "TutorialsPoint";
let len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// string containing the special characters
string1 = "Tutor!@l$Po!nT";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// String containing spaces.
string1 = "Welcome to the TutorialsPoint!";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);

On compiling, it will generate the following JavaScript code −

//  Regular string
var string1 = "TutorialsPoint";
var len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// string containing the special characters
string1 = "Tutor!@l$Po!nT";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// String containing spaces.
string1 = "Welcome to the TutorialsPoint!";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);

Output

The above code will produce the following output −

The length of `TutorialsPoint` is 14
The length of `Tutor!@l$Po!nT` is 14
The length of `Welcome to the TutorialsPoint!` is 30

Using the for loop to calculate the length of the string

Users can also create a custom function to find the length of the string in TypeScript. The custom function to find the string's length is useful for beginners because the interviewer might ask to find the string’s length without using the length property.

Syntax

Users can follow the syntax below to use the for loop to find the string’s length.

let string1: string = "TutorialsPoint";
let len = 0;
for (let char of string1) {
   len++;
}

Example

In the example below, we have created the ‘str’, string variable, and ‘len’, number variable. After that, we used the for of loop to iterate through every character of the string. While iterating through the string using for of loop, we add the 1 to the len variable for every character of the string.

At last, we are returning the final value of the len variable as the length of the parametrized string from the get_len() function.

//  function to find the length of the string
function get_len(str: string): number {
   // Initalize the len variable with zero
   let len: number = 0;
   //   Iterate through the every character of the string,
   // and for every character add 1 to the len variable.
   for (let char of str) {
      len++;
   }
   //   return len variable
   return len;
}
let my_name: string = "Shubham";
// call the get_len() function
let name_len: number = get_len(my_name);
// Print the string.
console.log("The length of `" + my_name+ "` is " + name_len);

On compiling, it will generate the following JavaScript code −

//  function to find the length of the string
function get_len(str) {
   // Initalize the len variable with zero
   var len = 0;
   //   Iterate through the every character of the string,
   // and for every character add 1 to the len variable.
   for (var _i = 0, str_1 = str; _i < str_1.length; _i++) {
      var char = str_1[_i];
      len++;
   }
   //   return len variable
   return len;
}
var my_name = "Shubham";
// call the get_len() function
var name_len = get_len(my_name);
// Print the string.
console.log("The length of `" + my_name + "` is " + name_len);

Output

The above code will produce the following output −

The length of `Shubham` is 7

Example

We have manipulated the above example in the example given below. We are counting only the total number of alphabetic characters in the string. We have used the chareCodeAt() method of the string class to get the ASCII value of the character and excluded the characters from the string length based on the character’s ASCII value.

In the output, users can observe that the get_len() function returns the string length after excluding the special characters from the string.

function get_len(str: string): number {
   let len: number = 0;
   for (let i = 0; i < str.length; i++) {
   if (
      (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) ||
      (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122)
   ) {
      len++;
   }
   }
   return len;
}
let test: string = "RBKQW ASNQW @!@#!@";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));
test = "TutorialsPoint";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));

On compiling, it will generate the following JavaScript code −

function get_len(str) {
   var len = 0;
   for (var i = 0; i < str.length; i++) {
      if ((str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) ||
         (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122)) {
         len++;
      }
   }
   return len;
}
var test = "RBKQW ASNQW @!@#!@";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));
test = "TutorialsPoint";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));

Output

The above code will produce the following output −

The length of `RBKQW ASNQW @!@#!@` excluding the special characters is 10
The length of `TutorialsPoint` excluding the special characters is 14

We learned two different methods to find the length of the string. In the first method, we simply used the length property of the string class. In the second method, we created the custom function and manipulated the example to exclude the special characters from the string.

Updated on: 19-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements