Results 1 to 5 of 5

Thread: Length of Strings

  1. #1

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    In JavaScript how do you find the length of a string.
    I've seen the way to do it is
    stringname.length
    but this always brings back an error.
    I've also tried to do length(stringname) and Len(stringname).

  2. #2
    Guest
    There's probably an easier way...but one thing that will work is to create a string object so you can use the length property. You could wrap this in a function to deal with other string values like this:
    Code:
    <HTML>
    <HEAD>
    <TITLE>Test</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function getLen(someString)
      {
      var str = new String();
      str = someString;
      
      return str.length;
      }
    // -->
    </SCRIPT>
    </HEAD>
    <BODY LINK="#000080" VLINK="#000080">
    
    <P><BUTTON onClick="alert(getLen('Hello World!'))">Click Me</BUTTON></P>
    
    </BODY>
    </HTML>
    You could use a function like this to get the lengths of strings that weren't declared as string objects...like values from INPUTs or something.

    Paul

  3. #3

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    This is a cut down version of my code.

    var selvalue=s.value + "J";
    For (N = 0; N<selvalue.length; N++)
    {
    }

    and i keep getting the error of object expected. Maybe I've done something wrong in my loop, but whenever I take the loop out, the error disappears.

  4. #4

    Thread Starter
    Hyperactive Member richy's Avatar
    Join Date
    Jan 1999
    Location
    Liverpool, England
    Posts
    353
    from doing a few tests now, I've found out it is my loop thats giving me the error, so can anyone tell me what's wrong with it?

    P.S. I have declared N up above.

  5. #5
    Guest
    I think you'd need to declare 'selvalue' as a string object to use the length property, as in the example I posted for you. I don't think a 'var' has the length property. That's probably why it's telling you that it's expecting an object.

    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width