1 / 10

String, Math & Miscellaneous Methods & Properties

Learn about String methods like indexOf, lastIndexOf, substr, substring, and their applications. Discover Math and Miscellaneous methods in JavaScript. References included.

Download Presentation

String, Math & Miscellaneous Methods & Properties

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String, Math & Miscellaneous Methods & Properties

  2. anchor( ) big( ) blink( ) bold( ) charAt( ) fontcolor( ) fontsize( ) indexOf( ) italics( ) lastIndexOf( ) link( ) small( ) sub( ) sup( ) substr(start, length) substring(start, end) toLowerCase( ) toUpperCase( ) String Object Methods & Properties length is a property of a string - so, no ( )

  3. Example: indexOf • Purpose: locate a string in another string • Let x be a string variable (maybe from some kind of input operation) that currently has the value: "DJ Foreman" • We want to see if it contains an "F" • J = x.indexOf("F"); • J now has the position of the "F" • Remember that we start counting at ZERO • So J now has the value of 3

  4. Example: lastindexOf, length <html> <script language="javascript"> X="DJ Foreman, Ph. D."; document.write(X.lastIndexOf(" ")+"<br>"); document.write(X.length); </script> </html> Result is: 15 17

  5. Example of substr • Purpose: copy a piece of a string, given its start and length • Let X be a string variable (maybe from some kind of input operation) that currently has the value: "DJ Foreman, Ph. D." • If we want the "Foreman": • M=X.indexOf(" ")+1; // finds the 1st char after the blank • N=X.indexOf(","); // finds the comma • L=N-M; // computes the length • Y=X.substr(M, L); // gets the "Foreman" into Y

  6. Example of substring • Purpose: copy a piece of a string, given its start and ending position • Let X be a string variable (maybe from some kind of input operation) that currently has the value: "DJ Foreman, Ph. D." • If we want the "Foreman": • M=X.indexOf(" ")+1; // finds the 1st char after the blank • N=X.indexOf(","); // finds the comma • Y=X.substring(M,N); // gets the "Foreman" into Y

  7. abs( ) ceil( ), floor( ) exp( ) log( ) max( ), min( ) pow( ) random( ) round( ) sqrt( ) sin(radians), asin( ) cos( ), acos( ) tan( ), atan( ) Some Math Object Methods

  8. Some Math Object Properties • E • LN2, LN10 • LN2E, LOG2E • PI • SQRT2 • SQRT1_2 (sqrt of 1/2) ex: Math.PI These are all constants

  9. Some Misc Methods • blur( ) • focus( ) • select( ) • eval( ) • parseInt( ) or ParseFloat( ) if var x="10"; y="5"; x+y gives "105" parseInt(x)+parseInt(y) gives 15

  10. Some Useful References • "Javascript by Example" (various publishers, if you can find it) • "How to Set Up and Maintain a WWW Site", Addison Wesley • "Essential Javascript for Web Professionals", Prentice Hall • "Essential CSS & DHTML for Web Professionals", Prentice Hall

More Related