HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

The length property is kind of cool, but the string object has a lot more up its sleeve. Objects also have methods. Strings in JavaScript have all kinds of methods (things they can do). Here are a few favorites:

  • toUppercase() makes an entirely uppercase copy of the string.

  • toLowercase() makes an entirely lowercase copy of the string.

  • substring() returns a specific part of the string.

  • indexOf() determines whether one string occurs within another.

The string object has many other methods, but these are highlighted because they're useful for beginners. Many string methods, such as big()and fontcolor(), simply add HTML code to text. They aren't used very often because they produce HTML code that won't validate, and they don't really save effort. Some other methods, such as search(), replace(), and slice(), use advanced constructs like arrays and regular expressions that aren't necessary for beginners.

Like properties, methods are attached to an object by the period. Methods are distinguished by a pair of parentheses, which sometimes contain special information called parameters.

The best way to see how methods work is to look at some in action. Look at the code for stringMethods.html:

 <script type = "text/javascript">
  //from stringMethods.html
  var text = new String;
  text = prompt("Please enter some text.");
  alert("I'll shout it out:");
  alert("Now in lowercase...");
  alert("The first 'a' is at letter...");
  alert("The first three letters are ...");
  alert(text.substring(0, 3));

In this example, the text was explicitly defined as a string variable by saying

var text = new String;

JavaScript does not require you to explicitly determine the type of a variable, but you can do so, and this is sometimes helpful.

Here's another cool thing about Komodo Edit. When you type text, Komodo understands that you're talking about a string variable and automatically pops up a list of all the possible properties and methods of the string object.

You can see from the preceding code that methods are pretty easy to use. When you have a string variable, you can invoke the variable name followed by a period and the method name. Some methods require more information to do their job. Here are the specifics:

  • toUppercase() and toLowercase() take the value of the variable and convert it entirely to the given case. This method is often used when you aren't concerned about the capitalization of a variable.

  • indexOf(substring) returns the character position of the substring within the variable. If the variable doesn't contain the substring, it returns the value -1.

  • substring(begin,end) returns the substring of the variable from the beginning character value to the end.

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: