jQuery

How to count number of characters in a string with JQuery

In this tutorial, we are going to see how to count number of characters in a string with JQuery. JQuery’s .length property can be used to get the number of characters in a string. You can also use this property to calculate the number of items in a jQuery object.
 

How to count number of characters in a string with JQuery
$(document).ready(function(){
   var str = "      Lorem ipsum dolor       fat amet,    consectetur   adipiscing muot.       ";
   console.log(str.length); 
   console.log($.trim(str).length); 
   console.log(str.replace(/ /g,'').length); 
});

Output:

80
67
49
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *