How To Count Number of Characters Present in a Given String Using JavaScript.

0

In this article we’ll count the number of characters present in string using JavaScript. Master character counting in JavaScript! This guide explores efficient methods to count characters with and without spaces from a given string, empowering you to tackle various text processing tasks.


count the number of characters present in the string


First we’ll count the characters' presence in the string including space. A simple way to count the length of the string.


JavaScript


    let string = "   Hello  welcome to  maketechstuff.com ";

    let output = string.length;
    console.log(string);
    console.log(output);
  


Output:


count the number of characters including space present in a string


If you want to count only the characters in a string, excluding spaces, you'll need to remove all spaces from the string before counting. 


Here are three approaches to do that:


Approach 1:

  1. Divide the string into an array using the split() method, using spaces as delimiters.
  2. Combine all elements of the array into a single variable through for loop.
  3. Calculate the length of the resulting string using length property.


JavaScript


    let string = "   Hello  welcome to  maketechstuff.com ";

    let output = "";
    let split = string.trim().split(" ");
    for (let i = 0; i < split.length; i++) {
        const element = split[i];
        output += element;
    }
    console.log(string);
    console.log(output);
    console.log(output.length);
  

Output:


count only characters from a given string, excluding the spaces


Approach 2:

Determine the length of the string after removing all spaces using the replace method and a regular expression.


JavaScript


    let string = "   Hello  welcome to  maketechstuff.com ";

    let output = string.replace(/\s+/g, "");

    console.log(string);
    console.log(output);
    console.log(output.length);
  

Output:


count only characters from a given string excluding, the spaces


Approach 3:

First, we'll split the string from space, making into an array using the split method.

Next, we'll create a new array using the filter method. This method excludes empty elements, effectively removing all empty elements from the array and create a new array.

To determine the number of characters, we can join the array elements back into a string using the join method and then count the length of the resulting string.


JavaScript


    let string = "   Hello     welcome to  maketechstuff.com    ";

    let output = "";
    let array = string.trim().split(" ");
    let new_array = array.filter(function (array) { return array })
    output = new_array.join("");

    console.log(string);
    console.log(output);
    console.log(output.length);
  

Output:

count only characters from a given string excluding, the spaces


So that's how you can count the characters from the given string using JavaScript. If you have any questions or suggestions you can write in the comment section.


You may like:

  1. How to create character counter with and without including space using JavaScript.
  2. How to count the number of words present in a string using JavaScript.
  3. How to create a live word counter using JavaScript.
  4. Detect and count the number of spaces in string using JavaScript.

Tags

Post a Comment

0Comments

Share your thoughts.

Post a Comment (0)
To Top