javaScript

Welcome To JavaScript 

This tutorial will take you step by step through the fundamentals of Javascript

You will learn how to write 

String Length

The length of a string (a string object) is found in the built in property length:


<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = txt.length;
</script>

</body>
</html>

 output=26


document.getElementById("demo").innerHTML = txt.length;

Finding a String in a String

The indexOf() method returns the position (a number) of the first occurrence of a specified text (string) in a string:
 

<!DOCTYPE html>
<html>
<body>

<p id="p1">Please    locate where 'locate' occurs!.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = document.getElementById("p1").innerHTML;
    var pos = str.indexOf("locate");
    document.getElementById("demo").innerHTML = pos;
}
</script>

</body>
</html>
 
output=10 

NOTE:

Did you notice that the indexOf() method and the search() method returned the same value?
The two methods look similar, but the search() method can take much more powerful search values.
You will learn about that in the chapter about regular expressions.
















www.jsfiddle.net

1 comment:

Related Posts Plugin for WordPress, Blogger...