IT-Academy

JS String

Mar 10th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <script type="text/javascript">
  3.    
  4.     //string declaration
  5.     phrase1 = "He said \"Hello world\" "; //to write quotes into string use \
  6.     phrase2 = 'Hello USA ';
  7.     phrase3 = new String("Hello New York");
  8.    
  9.     document.write("<h3>Declaration</h3>");
  10.     document.write("Phrase1: " + phrase1 + "<br />");
  11.     document.write("Phrase2: " + phrase2 + "<br />");
  12.     document.write("Phrase3: " + phrase3 + "<br />");
  13.     document.write("Phrase1 + 2 + 3 : " + phrase1 + phrase2 + phrase3 + "<br />");
  14.    
  15.    
  16.     document.write("<h3>Methods and properties</h3>");
  17.     document.write("Phrase1 length: " + phrase1.length + "<br />");
  18.     document.write("Phrase1 Upper Case: " + phrase1.toUpperCase() + "<br />");
  19.     document.write("Phrase1 Lower Case: " + phrase1.toLowerCase() + "<br />");
  20.     document.write("Phrase1 char at index 10: " + phrase1.charAt(10) + "<br />");
  21.     document.write("Phrase1 index of \"el\": " + phrase1.indexOf("el") + "<br />");
  22.     document.write("Phrase1 slice from index 9 to 15: " + phrase1.slice(9,15) + "<br />");
  23.     document.write("Phrase1 replace Hello with Hi: " + phrase1.replace("Hello","Hi") + "<br />");
  24.    
  25.     array = phrase1.split(" "); //Split string and save to array
  26.    
  27.     document.write("<h3>Compare</h3>");
  28.     phrase1 = "abcd";
  29.     phrase2 = "Abcd";
  30.    
  31.     if (phrase1 == phrase2) {
  32.         document.write(phrase1 + " and " + phrase2 + " are equal!" + "<br />");
  33.     } else {
  34.         document.write(phrase1 + " and " + phrase2 + " are NOT equal!" + "<br />");
  35.     }
  36.    
  37.     phrase1 = "apple";
  38.     phrase2 = "ball";
  39.    
  40.     if (phrase1 < phrase2) {
  41.         document.write(phrase1 + " is less then " + phrase2 + "<br />");
  42.     } else {
  43.         document.write(phrase2 + " is less then " + pharse1);
  44.     }
  45.     </script>
Advertisement
Add Comment
Please, Sign In to add comment