How to Simulate Multiple Pages with Tabbed Panels
How to Create a Mobile-Ready Page in Dreamweaver
How to Create a Data Table for Your Website

How to Use JavaScript's String Methods to Manipulate Text for HTML5 and CSS3 Programming

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:

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

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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Install jQuery
How to Throw Errors in JavaScript to Program with HTML
Basic I/O Commands in JavaScript
Inserting Content Before, After, and Inside Elements with jQuery
How to Draw Shapes on JavaScript's Canvas for HTML5 and CSS3 Programming
Advertisement

Inside Dummies.com