When developing for the web strings are widely used because the text is the basis of communication between client and server sending HTML forms. Javascript offers good support to the text through the String: a string is nothing more than a set of characters enclosed in single or double quotes.
We can instantiate a new String object through this syntax
var strText = new String (“Hello World!”);
or simply declare
var strText = “Hello world!”
In JavaScript you can use either double that of single quotes, we resort to the latter usually when you use the scripting language within the HTML
<a href=”#” onClick=”alert(‘Only an example!’)”>Example </a>
In this situation, the use of double quotes would be confusing the browser that interprets erroneously the value of onClick. Another problem is the use of apostrophe in a string in the value of alert (in our example above), and are therefore used the so-called escape characters, particular pairs of symbols that are used to represent characters without representation simple or otherwise in conflict with the syntax of JavaScript
<a href=”#” onClick=”alert(‘My examples\’!’)”>Example </a>
Escape characters have the form \x, where instead of x we have the symbol to represent. The most commonly used escape characters are
- \ ‘Quotes
- \ “Double quotes
- \ \ Backslash
- \ R carriage return
- \ No newline
- \ T tab
The String object has a large number of useful methods for manipulating strings, basic is also the length property, which represents the number of characters that make up the sequence. The most commonly used methods are:
- chartAt(n) returns the character index n
- charCodeAt(n) that returns the Unicode code of the character of index n
- indexOf(ss), which returns the position of the first occurrence of the substring ss, or -1 if there is no occurrence
- lastIndexOf(ss), which returns the position of the last occurrence of the substring ss, or -1 if there is no occurrence
- split(ss) that splits a string into an array of sub-strings, obtained by disrupting the invocation object to every occurrence of ss
- substring(ci, cf) that returns a sub-string found in the object of invocation from the character index we included up to that cf excluded
- toLowerCase() that returns a new string with all lowercase letters
- toUpperCase() that returns a new string with all uppercase
For a more detailed illustration of the methods of the String object can visit this page.
Unlike other objects, strings may be used by some operators, such as + for concatenation or Boolean operators. To show in practice how to use the structures presented so far is an example:
<html> <head> <title>Example Page</title> <script language="javascript"> var s1 = "Hello"; var s2 = "good afternoon"; var s3 = "hello"; var s4 = s1 + " " + s2 + " " + s3; alert(s4); s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); s3 = s3.toLowerCase(); s4 = s1 + " " + s2 + " " + s3; alert(s4); if (s1 == s2) alert("First and second strings are the same"); else if (s1 == s3) alert("First and third strings are equal"); else if (s2 == s3) alert("Second and third strings are the same"); else alert("Strings are different"); </script> </head> <body> </body> </html>
I tried to include all the concepts seen so far to show the use of the methods listed on strings, however the most common use of the String object in JavaScript is to validate inputs of the HTML form. As an exercise you can validate an email address looking for the presence of the @ character in the string.