Javascript Len() string Length


This article is written by Pon Saravanan  on 21-Jul-07 Last modified on :07-Oct-09





Javascript Length String

While working with strings often we need to find a length of string, count of charachers etc. Basically a developer who has a very good knowledge in vb may look for the equivalent of len() in javascript as in VB.  Which is primarily used for finding the length of string.

In Javascript the length is a property

we dont have any functions for finding lenth in String Object. But we have a property called Length in all string objects. and can be called as follows [variable]. [function name.] In this Case it is strInput.length();

function Len(strInput)
{
      return strInput.length;
}

self.alert(len(' test this string'))
self.alert(len("test this single's string"))

If you are placing this function in a common JavaScript file or anywhere in the same page’s script block. Then we can straight away call the Len function as we do in VB.

Like i said now you can check the length of the string in the VB like alert(len("testing")).
For various javascript string related functions, please check out javascript section.

Examples to find the length of string

var TestString = 'I'm having a single quote.';
This string has a single quote(') in it, so JavaScript needs an escaping character, Escape is nothing but telling the browser to treat the quote as a integral character inside the group. This can be done by placing a backslash \ before it:

var TestString = 'I\'m having a single quote.';

function Len(strInput)
{
      return strInput.length;
}
self.alert(len('I\'m having a single quote.'))

self.alert(len("I'm having a single quote."))

Now if you check the len(TestString) it will produce 26











Comments
  • GUEST
    12/25/2010 4:31:28 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Blue
Brown
Gray