Home arrow JavaScript arrow Page 5 - Stringing Things Along

Building Character - JavaScript

Think JavaScript's only good for image swaps and tickertapes?Think again - the language comes with a powerful String() objectdesigned to help you quickly and efficiently perform string manipulationtasks in the client. This article explains how, with illustrations andcode samples.

  1. Stringing Things Along
  2. Elementary School
  3. When Size Does Matter
  4. Slice And Dice
  5. Building Character
  6. Search And Destroy
  7. Bigger, Bolder, Better
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 10
January 22, 2003

print this article


You can use the indexOf() method to locate the first occurrence of a particular set of characters in a string
<script language="Javascript">
// set string
var str = "It's a bird! It's a plane! It's Superman!";
// returns 5
and the lastIndexOf() method to locate its last occurrence.
<script language="Javascript">
// set string
var str = "It's a bird! It's a plane! It's Superman!";
// returns 38
Both these functions return the position of the substring in the original string, or -1 if no match is found.

Of course, the indexOf() method only returns the first match - but what if you need to find the second or third match? Simply add an argument to the method call, indicating where in the string the indexOf() method should begin searching:
<script language="JavaScript">
// set string
var str = "It's a bird! It's a plane! It's Superman!";
// returns 18
alert(str.indexOf("a", 10));
You can use the charAt() method to obtain the character at a particular index position within the string.
<script language="JavaScript">
// set string
var str = "Batman and Robin";
// returns "t"
A common application of the charAt() method is to iterate through a string, checking each character for validity against a pre-defined list. Consider the following example, which demonstrates how this works in the context of a password validator:
<script language="JavaScript">
// verify password string against allowed list of characters function
checkPassword() {
// error flag
var error = 0;
// list of valid characters
var validChars = "abcdefghijklmnopqrstuvwxyz1234567890";
// get value from form
var password = document.forms[0].password.value;
// iterate through string and check against allowed character
for (x=0; x<password.length; x++)
c = password.charAt(x);
if (validChars.indexOf(c) == -1)
error = 1;
// display appropriate message
if (error == 1)
alert ("Error!");
return false;
alert ("All OK!");
return true;
Enter password: <input name="password" type="text">
<input type="submit" value="Check!" onClick="checkPassword()"> </form>

>>> More JavaScript Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates


Dev Shed Tutorial Topics: