JavaScript Tutorial/String/length

Материал из Web эксперт
Перейти к: навигация, поиск

Get the lengh of a string

<html>
<head>
<script type="text/javascript" language="javascript">
<!-- //
onload = function(){
 var myString = "AAA";
 document.write(myString + " has a length of " + myString.length + " characters.");
 }
// -->
</script>
</head>
<body>
</body>
</html>


string.length

The length property of an instance of the String object returns the total length of the string.



<html>
    <script language="JavaScript">
    <!--
    var myString1 = new String("Hello, World");
    var myString2 = new String("Here is a longer string");
    var myString3 = new String("Here is an even longer string");
    document.write(myString1 + ": is " + myString1.length);
    document.write(" characters long.<br>");
    document.write(myString2 + ": is " + myString2.length);
    document.write(" characters long.<br>");
    document.write(myString3 + ": is " + myString3.length);
    document.write(" characters long.<br>");
    document.close();
    -->
    </script>
</html>