How to find the number of characters in a string using jQuery

Topic: JavaScript / jQuery« Prev|Next »

Answer: Use the jQuery .length property

The jQuery .length property can be used to get or find out the number of characters in a string. You can also use this property to calculate the number of elements in a jQuery object.

  • <script type="text/javascript">
  •     $(document).ready(function(){
  •         var myStr = "      Paragraph of text with   multiple white     spaces before and after.       ";
  •         myStr.length; // Returns 81
  •         $.trim(myStr).length; // Returns 68
  •         myStr.replace(/ /g,'').length; // Returns 53
  •     });
  • </script>

String Length Calculator

Enter the string of text in the textarea and click the buttons.


Related FAQ

Here are some more FAQ related to this topic:


Your Feedback:


We would love to hear from you! Please say something.