Techniques for text String Management

When you load a text string into a Variable, the Variable automatically gains certain Methods which you can use to make changes to the text. These are called string Methods.

The charAt() Method

A good example of this is the charAt() Method, which tells you what letter (or "character") is at a given position in the text string. The term "position" refers to its numerical position in the sequence of text, and as usual, you start counting with 0. That means the first letter in a text string is at position 0. Here's how you use the charAt() Method:

var charAtStr = prompt("Please enter a sentence:", "Let's see what happens!");
var pos = prompt("Please enter the character position you are looking for:", 10);

alert('"' + charAtStr + '".charAt(' + pos + ') = ' + charAtStr.charAt(pos));

To try this Function yourself, click this button:

As with all of the text string Methods discussed in this section, the charAt() Method takes an argument and returns a value. In this case, the argument is a number position and the returned value is the character in the text string that is at that position.

More text string Methods

 The indexOf() Method

The indexOf() Method works like the charAt() Method in reverse. It takes a small text string as an argument and returns a number indicating the position of the small text string within a bigger text string. If the small text string is not present in the bigger text string, the indexOf() Function returns the number -1.

var indexOfStr = prompt("Please enter a sentence:", "Let's see what happens!");
var str = prompt("Please enter the search string you are looking for:", "at");
var start = prompt("Where do you want to start?", 0)

alert('"' + indexOfStr + '".indexOf(' + str + ') = ' + indexOfStr.indexOf(str, start));

Try it yourself yourself, click this button:

The substring() Method

With the substring() Method, you can select and extract chunks of text based on their numerical positions within larger text strings.

var subStr = prompt("Please enter a sentence:", "Let's see what happens!");
var start = prompt("Where do you want to start?", 6)
var end = prompt("Where do you want to end?", 11)

alert('"' + subStr + '".substring(' + start + ", " + end + ') = ' + subStr.substring(start, end));

Try it yourself, click this button:

How to use string Methods

Suppose you want pull some information off of a form on a Web page. In this case it's a phone number and people often put their area code in with their phone number, even though there is a separate entry on the form for that. Here's a Function that will separate the area code from the phone number. (Note that we are detecting the area code by searching for the "(" character. This may add some confusion when you look at this since we use a lot of parentheses in the Function already.):

function checkNumber(num) {
      var phone, areaCode = "No Area Code";

      if (num.indexOf("(") ! = -1) {
            areaCode = num.substring(num.indexOf("(") + 1, num.indexOf(")"));
            phone = num.substring(num.indexOf(")") + 1, num.length);
      }

      alert("Area Code = " + areaCode + " Phone Number = " + phone + ".")
}

In order to keep this example simple, it doesn't perform multiple tests. For instance, this script could also have detected area codes written in the following formats: 123-456-7890, or 123.456.7890, by using other combinations of string Methods.