100 likes | 115 Views
Learn about String methods like indexOf, lastIndexOf, substr, substring, and their applications. Discover Math and Miscellaneous methods in JavaScript. References included.
E N D
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 ( )
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
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
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
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
abs( ) ceil( ), floor( ) exp( ) log( ) max( ), min( ) pow( ) random( ) round( ) sqrt( ) sin(radians), asin( ) cos( ), acos( ) tan( ), atan( ) Some Math Object Methods
Some Math Object Properties • E • LN2, LN10 • LN2E, LOG2E • PI • SQRT2 • SQRT1_2 (sqrt of 1/2) ex: Math.PI These are all constants
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
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