JavaScript basics
JavaScript gets the judgment of the string length
To determine the length of a string in JavaScript, you can use length to obtain it. It is similar in concept to the strlen() function in PHP . Both return numbers. Please see the example below.
<script language="JavaScript">
var string = "test string";
document.write(string.length);
</script>
var string = "test string";
document.write(string.length);
</script>
In this example, we set a string string as test string, and calculate the string length through string.length. The output result is 11 (including blank characters). Length judges the number of characters, not the number of letters. , So blank characters will also be counted. Pay special attention to this point. It is recommended that the web design adopts UTF-8 universal code to design, it is less likely to cause the problem of wrong judgment of length character length due to encoding. document.write is only used to display text on the web page.
Post a Comment
0 Comments