Press "Enter" to skip to content

String Examples in JavaScript

Definition :

    It is an object of java script which provides the methods for performing validation kind of functions from the web page. Which is meant for storing the data in linear manner. String holds huge set of characters in linear manner.

Program 1 :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=”hello”;

                                                   b=new String(“HELLO”);

                                                   document.write(a+”</br>”);

                                                   document.write(b+”</br>”);

                                                 }

                                </script>

                </head>

                <body>

                     <input type=”button” value=”click” onclick=”display()”/>

                </body>

</html>

Program 2 :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=”hello”;

                                                   document.write(a.toUpperCase()+”</br>”);

                                                   document.write(a.toLowerCase()+”</br>”);

                                                   document.write(a.charAt(1)+”</br>”);

                                                   document.write(a.concat(“HI”)+”</br>”);

                                                   document.write(a.indexOf(“l”)+”</br>”);

                                                   document.write(a.lastIndexOf(“l”)+”</br>”);

                                                   document.write(a.substring(1,4)+”</br>”);

                                                   document.write(a.substr(1,4)+”</br>”);

                                                 }

                                </script>

                </head>

                <body>

                     <input type=”button” value=”click” onclick=”display()”/>

                </body>

</html>

Program 3 :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=”hello hi”;

                                                   document.write(a.search(“e”)+”</br>”);

                                                   document.write(a.replace(“e”,”p”)+”</br>”);

                                                   document.write(a.split(” “).reverse().join(” “));

                                                 }

                                </script>

                </head>

                <body>

                     <input type=”button” value=”click” onclick=”display()”/>

                </body>

</html>

Program 4 :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   a=”hello    “;

                                                   document.write(a.length+”</br>”);

                                                   document.write(a.startsWith(“h”)+”</br>”);

                                                   document.write(a.endsWith(“o”)+”</br>”);

                                                   document.write(a.trim().length+”</br>”);

                                                  }

                                </script>

                </head>

                <body>

                     <input type=”button” value=”click” onclick=”display()”/>

                </body>

</html>

Program 5 :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   c=0;

                                                   a=”hello”;

                                                   b=prompt(“enter a character”, “0”);

                                                   for(i=0;i<a.length-1;i++)

                                                   {

                                                     if(a.charAt(i)==b)

                                                                 c++;

                                                   }

                                                   alert(“character “+b+” available “+c+” times”);

                                                  }

                                </script>

                </head>

                <body>

                     <input type=”button” value=”click” onclick=”display()”/>

                </body>

</html>

Program 6 :

<html>

     <head>

                     <script language=”javascript”>

                                     function display()

                                                 {

                                                   uname=document.getElementById(“uname”).value;

                                                   pwd=document.getElementById(“pwd”).value;

                                                   eml=document.getElementById(“eml”).value;

                                                   phno=document.getElementById(“phno”).value;

                                                   addr=document.getElementById(“addr”).value;

                                                   if(isNaN(phno))

                                                      alert(“Invalid phone Number”);

                                                   if(phno.length!=10)

                                                      alert(“phone number length should be 10 digits only”);

                                                   else if(uname.trim().length<5)

                                                      alert(“please enter 5 characters in username field”);

                                                   else if(pwd.trim().length<5)

                                                      alert(“please enter atleast 5 characters in password field”);

                                                   else if(addr-trim().length<5)

                                                      alert(“please enter atleast 5 characters in address field”);

                                                   else if(eml.trim().indexof(“@”)<2||

                                                       (eml.trim().indexof(“@”)!=eml.trim().lastIndexof(“@”)))

                                                                  alert(“please enter valid mail id”);

                                                    else

                                                                   alert(“user registered successfully”);

                                                }

                                </script>

                </head>

                <body>

                     <form>

                                     username:-<input type=”text” id=”uname”/> <br>

                                                 password:-<input type=”text” id=”pwd”/> <br>

                                                 email:-<input type=”text” id=”eml”/> <br>

                                                 phone no:-<input type=”text” id=”phno”/> <br>

                                                 address:-<textarea rows=”10″ cols=”10″ id=”addr”> </textarea> <br>

                                                 <input type=”button” value=”Register” onclick=”display()”/>

                                                 <input type=”reset” value=”cancel”/>

                                </form>

                </body>

</html>

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *