JavaScript String

It is an object which is a combination of sequence of character. We define it in two way:

  • By using lateral (using double quotes)

  • By using  “new” keyword

 

Ex.

By using lateral (using double quotes)

<script>  

    var str="Hi";  

    document.write(str);  

</script> 

 

By using  “new” keyword

 

<script>  

     var str=new String("Hi, JS String");  

     document.write(str);  

</script>

JavaScript string methods:

trim(), slice() toLowerCase(), toUperCase(), indexOf(“string value to find index”) charAt(number which string char you want to find), replace(), match(), concat().

 

Ex.1

<script>  

    var str="naveenkumar";  

    document.write(str.charAt(2));  

</script>

 

Result:

D

Ex.2

 

<script>  

    var s1="naveenkumar";  

    var s2=s1.slice(2,5);  

    document.write(s2);  

</script> 

Powered  by Codersarts