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 />

string.slice(startIndex [, endIndex])<br />

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

Compatibility ✓ ✓ ✓ ✓ ✓<br />

Example<br />

With Listing 34-5, you can try several combinations of parameters with the<br />

string.slice() method (see Figure 16-3). A base string is provided (along with<br />

character measurements). Select from the different choices available for parameters<br />

and study the outcome of the slice.<br />

Listing 34-5: Slicing a String<br />

<br />

<br />

String Slicing and Dicing, Part I<br />

<br />

var mainString = “Electroencephalograph”<br />

function showResults() {<br />

var form = document.forms[0]<br />

var param1 = parseInt(form.param1.options[form.param1.selectedIndex].value)<br />

var param2 = parseInt(form.param2.options[form.param2.selectedIndex].value)<br />

if (!param2) {<br />

form.result1.value = mainString.slice(param1)<br />

} else {<br />

form.result1.value = mainString.slice(param1, param2)<br />

}<br />

}<br />

<br />

<br />

<br />

String slice() Method<br />

<br />

Text used for the methods:<br />

Electroencephalograph<br />

----5----5----5----5-<br />

<br />

<br />

String MethodMethod ParametersResults<br />

<br />

string.slice()<br />

(&nbsp;<br />

0<br />

1<br />

2<br />

3<br />

5<br />

Continued<br />

479<br />

stringObject.slice()

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

Saved successfully!

Ooh no, something went wrong!