15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16 ✦ String and Number Objects (Chapters 34 and 35)<br />

Figure 16-1: Conversions from text characters to ASCII values and<br />

vice versa<br />

string.indexOf(searchString [, startIndex])<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

Enter each of the following statements (up to, but not including the “//” comment<br />

symbols) into the top text box of The Evaluator (you can simply replace the parameters<br />

of the indexOf() method for each statement after the first one). Compare<br />

your results with the results shown below.<br />

a = “bananas”<br />

a.indexOf(“b”) // result = 0 (index of 1st letter is zero)<br />

a.indexOf(“a”) // result = 1<br />

a.indexOf(“a”,1) // result = 1 (start from 2nd letter)<br />

a.indexOf(“a”,2) // result = 3 (start from 3rd letter)<br />

a.indexOf(“a”,4) // result = 5 (start from 5th letter)<br />

a.indexOf(“nan”) // result = 2<br />

a.indexOf(“nas”) // result = 4<br />

a.indexOf(“s”) // result = 6<br />

a.indexOf(“z”) // result = -1 (no “z” in string)<br />

473<br />

stringObject.indexOf()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!