1. Technology

Learn Modern Unobtrusive JavaScript



Apart from numbers, strings of text are the objects you are most likely to use in your scripts. As with numbers, there are two ways to define a string, one of which doesn't require an actual object declaration. Also as with numbers the difference between the two declarations is not something that will make a difference most of the time and so you may as well use the shorter declaration. Just about all of the times where the object version of a string is required the string will be converted automatically so that you don't have to worry about whether the text is actually an object or not. Here are examples of the two ways to define a variable containing text.

var text1 = "some text";
var text2 = new String("some more text");

As you can see, the difference between creating a number and creating a string is that the content of the string is enclosed in quotes. While my examples show the use of double quotes, you can use single quotes in JavaScript just as easily. The only thing you have to make sure of is that the quoters around your string of text are both the same type. You can easily contain the other type of quote from the one you use around your text within the text itself so if the text contains one type of quote you can use the other type around it. Where the text contains both types you will have to "escape" the one used around the text where it appears in the text by preceding it with a \ character.

Two text strings can be joined together (concatenated) using + between them like this:

var text3 = text1 + ' ' + text2;

Assuming the tree lines of code from the examples so far text3 contains some text some more text.

The string object supports quite a few methods for manipulating strings of text. The following are some of the more useful.

  • charAt(number) selects the single character at the specified position within the string
  • indexOf(substring) finds the position where the specified substring starts
  • lastIndexOf(substring) finds the last occurrence of the substring within the string
  • toLowerCase() convert the string to lowercase
  • toUpperCase() convert the string to uppercase
  • charCodeAt(number) selects the single character at the specified position within the string and returns the ASCII code equivalent
  • concat(string2) concatenates string2 onto the end of the string
  • fromCharCode(code) converts the ascii code back into the character that it represents, this is a static method not associated with any string
  • localeCompare(string2) compares the string with string2 and returns -1, 0, or 1 depending on the sort sequence at the default locale
  • match(regexp) searches for substrings within the string that match the regular expression and returns them in an array
  • replace(regexp,string2) searches for substrings within the string that match the regular expression and replaces them with string2
  • search(regexp) returns true if a substring within the string matches the regular expression
  • split(char) splits the string at each occurrence of the specified character and puts them into an array (alternatively you can specify a regular expression instead of the char)
  • substr(start,end) gets the specified part of the string
  • toLocaleLowerCase() converts the string to lower case taking into account the default locale
  • toLocaleUpperCase() converts the string to upper case taking into account the default locale

There are further methods other than those I have listed here but they are either ones that are so old that the functionality that they perform is now handled by stylesheets or they are so new that all browsers are yet to support them.

You will have noticed that a couple of the methods are static. What this basically means is that you use them with the base string object itself eg. String.fromCharCode(65) rather than using them with your own string eg. text1.charCodeAt(3) and 'some text'.toUpperCase() as the static methods are independent of the actual string you are processing in what they do.

While comparisons on numbers are done by comparing the size of the numbers themselves eg. 3 < 10, comparisons on text strings are done by comparing the ascii codes corresponding to the characters within the text starting with the left most character and processing until the result can be determined eg '10' < '3'.

More of This Tutorial

  1. About.com
  2. Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Learn Modern JavaScript
  6. Unobtrusive JavaScript - Strings

©2014 About.com. All rights reserved.