Prototype.js - SPLessons

Prototype.js Strings

Home > Lesson > Chapter 7
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Prototype.js Strings

Prototype.js Strings

shape Introduction

This chapter demonstrates about the Prototype.js Strings which enhance the string object with series of useful methods of a strings and following are the concepts covered in this chapter.
  • String Methods

String Methods

shape Description

Prototype enhance the series of methods these are ranges from trivial to complex and following are some string methods are briefly explained. empty() Is used to check if the string is empty or not, if string is empty then it returns true otherwise fals. The syntax for empty() as shown below. [code] string.empty(); [/code] The code below demonstrates the empty() as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var str = ''; alert("''.empty() : " + str.empty() ); alert("''.blank() : " + str.blank() ); var str = ' '; alert("' '.empty() : " + str.empty() ); alert("' '.blank() : " + str.blank() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. blank() Is used to check whether the string is blank  or not which means empty or may contain some white spaces the syntax for  the blank as shown below. [code] string.blank(); [/code] The code  below demonstrates the blank() as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var str = ''; alert("''.blank() : " + str.blank() ); str = ' '; alert("' '.blank() : " + str.blank() ); str = 'a'; alert("a.blank() : " + str.blank() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. capitalize() Is used to capitalize the first letter of the string and remaining are small letters, the syntax for capitalize() as shown below. [code] string.capitalize(); [/code] The code below demonstrates the capitalize() as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var str = 'hello'; alert("hello.capitalize() : " + str.capitalize() ); str = 'HELLO WORLD!'; alert("HELLO WORLD!.capitalize() : " + str.capitalize() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. camelize() Is used to convert string separated with dashes into camelCase equivalent like "SP-Lessons"  converted into "SPLessons". The syntax is as shown below. [code] string.camelize(); [/code] The code below demonstrates the camelize() as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var str = 'background-color'; alert("background-color.camelize() : " + str.camelize() ); str = '-moz-binding'; alert("-moz-binding.camelize() : " + str.camelize() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. endwith() Is used to check whether the string ending with sub string or not which returns the Boolean value. If the string ends with sub string then which returns true or else which returns false. The syntax as shown below. [code] string.endsWith( substring ); [/code] The code below demonstrates the endwith() as shown. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var str = 'slaughter'; alert("First Check : " + str.endsWith("ghter") ); alert("Second Check : " + str.endsWith("TATA") ); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. include() Is used to check whether string contains sub string or not which returns Boolean values. If string contain sub string which returns true or else which returns false. The syntax as shown below. [code] string.include(substring); [/code] The code below demonstrates the include() as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { var str = 'Prototype framework'; alert( "First Check :" + str.include('framework' ) ); alert( "Second Check :" + str.include('No framework' ) ); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. scan() Is used to allows iterating over every occurrence of the given pattern and returns the original string. The syntax as shown below. [code] string.scan(pattern, iterator); [/code] The code below demonstrates the scan() as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { str = "apple, pear, orange"; alert ("Actaul Alert"); alert ( str.scan(/\w+/, alert ) ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image.

Summary

shape Key Points

  • String enhance the string object with series of methods.
  • Include() checks whether string contain sub string or not.
  • capitalize() capitalize the first letter of the string.