JavaScript: Strings - an array of characters
This page demonstrates how Strings can be manipulated using JavaScript.
JavaScript code snippets listed by task and organised in a tutorial style each building on the previous example. New topics are introduced when required.
1. |
Working with strings: declare explicitly or implicitly. Create a String object explicitly using the 'var' statement and 'new' keyword with the String() constructor function. Or, create a string primitive implicitly using 'var' and assignment of a literal value. |
2. |
Concatenation with strings, concatenate strings of 'numbers', example: 12+8=128. Example of string concatenation using '+' operator. Example of string concatenation using '+=' operator.
JavaScript has a comprehensive range of operators for building expressions: JavaScript operators table. |
3. |
Examples of calling String object methods 'indexOf()' and 'substring()'. Where 'substring()' is an example of a method that can accept either one or two parameters. Note that these methods do not cause a 'state change' of the string. Above confirms that a string is just an 'array' of characters indexed from 0. |
4. |
Use of the String object property 'length'. Combined with the method 'charAt()' to find the last character in a string. Using method 'charCodeAt()' to find the Unicode value of a character. |
|
See the String object reference for list of essential methods and properties. |
|
5. |
The first 128 Unicode characters are compatible with the ASCII characters. |
6. |
A form to count the occurrence of a character in a string. Uses the String object method 'charAt()' with an 'if statement' nested in a 'for loop'. |
7. |
A form to encrypt a string by manipulating the Unicode values. Uses the String object methods 'charCodeAt()', 'toLowerCase()' and an array to hold the cipher. |
8. |
A form that builds on code snippet 7 above and provides the ability to decrypt the encrypted string. Example of a primitive 'substitution cypher'. |
9. |
Build on the above and learn about JavaScript and Maths. View code examples on JavaScript: Maths page. |
| Suggest a code snippet to add to this category. | |
Back to top.
Copyright © 2006-2010 justfigures.co.uk

