// length Returns the length of the string document.write("myStr.length: " + myStr.length); // output: 16 document.write("
"); // prototype Allows you to add properties and methods to an object // prototype is available for most objects // Syntax: object.prototype.name=value function car(make, model, year) { this.make = make; this.model = model; this.year = year; } var myCar = new car("Toyota", "Corolla", 2002); car.prototype.cost = null; myCar.cost = 20000; document.write("myCar.cost: " + myCar.cost); document.write("
"); //***************************************************** // *** String HTML Wrapper Methods *** // HTML wrapper methods return the string enclosed in the appropriate // HTML tags //***************************************************** document.write("***String HTML Wrapper Methods***"); document.write("
"); // Method Description // anchor() Creates an anchor // Syntax: string.anchor(name) - name is required - it is the name of the anchor var strName = "Hyperlink"; // this will be text of hyperlink alert(strName.anchor("Hyper")); // this will be anchor // string.big() Wraps string in tags var strBig = "Big" document.write("strBig.big(): " + strBig.big() + "
"); // string.blink() Wraps string in tags var strBlink = "blink - only works in Firefox & Opera" document.write("strBlink.blink(): " + strBlink.blink() + "
"); // only works // in Firefox & Opera // string.bold() Wraps string in tags var strBold = "bold" document.write("strBold.bold(): " + strBold.bold() + "
"); // string.fixed() Wraps string in tags so that string // displays in fixed pitch font var strFixed = "fixed" document.write("strFixed.fixed(): " + strFixed.fixed() + "
"); // string.fontcolor("color") Wraps string in tags // The value can be a color name (e.g. green), // an RGB value (e.g. rgb(0,255,0)), // or a hex number (e.g. #00FF00) var strfontcolor = "fontcolor" document.write("strfontcolor.fontcolor(green): " + strfontcolor.fontcolor("green") + "
"); // string.fontsize(size) Wraps string in tags so that string // displays in fontsize - size is required and can be from 1-7 var strfontsize = "fontsize" document.write("strfontsize.fontsize(7): " + strfontsize.fontsize(7) + "
"); // string.italics() Wraps string in tags var stritalics = "italics" document.write("stritalics.italics(): " + stritalics.italics() + "
"); // link() Creates an link // Syntax: string.link(url) - url is required - it is the url of the link // The method returns a string wrapped in tags: string var strLink = "Ideal Programmer"; // this will be text of hyperlink alert(strLink.link("http://idealprogrammer.com")); // this will be link // string.small() Wraps string in tags - displays string in small font var strSmall = "Small" document.write("strSmall.small(): " + strSmall.small() + "
"); // string.strike() Wraps string in tags - displays string in strkethrough font var strStrike = "Strike" document.write("strStrike.strike(): " + strStrike.strike() + "
"); // string.sub() Wraps string in tags - displays string in subscript font var strSub = "Subscript" document.write("strSub.sub(): " + strSub.sub() + "
"); // string.sup() Wraps string in tags - displays string in superscript font var strSuperscript = "Superscript" document.write("strSuperscript.sup(): " + strSuperscript.sup() + "
"); //***************************************************** // *** String Object Methods *** //***************************************************** // Method Description // charAt(index) Returns the character at the specified index - index is required // 0 is the first character and the last character in a string is string.length-1 var strCharAt = "This is a string."; document.write("strCharAt(0): " + strCharAt.charAt(0) + "
"); document.write("strCharAt.charAt(strCharAt.length - 1): " + strCharAt.charAt(strCharAt.length - 1)); // charCodeAt(index) Returns the Unicode of the character at the specified index var strcharCodeAt = "This is a string."; document.write("strcharCodeAt(0): " + strcharCodeAt.charCodeAt(0) + "
"); document.write("strcharCodeAt.charCodeAt(strcharCodeAt.length - 1): " + strcharCodeAt.charCodeAt(strcharCodeAt.length - 1) + "
"); // concat() Joins two or more strings, and returns a copy of the joined strings // SYNTAX: string.concat(string2, string3, ..., stringX) var strA = "Aeschylus "; var strB = "was a Greek playwright.
"; document.write(strA.concat(strB)); // fromCharCode() Converts Unicode values to characters // SYNTAX: String.fromCharCode(n1, n2, ..., nX) - This is a static method // The syntax is String.fromCharCode() and not str.fromCharCode document.write(String.fromCharCode(73, 68, 77, 75, 77)); // indexOf() Returns the position of the first found occurrence of a specified value in a string // SYNTAX: string.indexOf(searchstring, start) - start is optional var strIndexOf = "Einstein was a philosophical realist"; document.write("strIndexOf.indexOf(ein): " + strIndexOf.indexOf("ein") + "
"); document.write("strIndexOf.indexOf(philo): " + strIndexOf.indexOf("philo") + "
"); document.write("strIndexOf.indexOf(real): " + strIndexOf.indexOf("real") + "
"); // lastIndexOf() Returns the position of the last found occurrence of a specified value in a string // SYNTAX: string.lastIndexOf(searchstring, start) - start is optional var strlastIndexOf = "Einstein was a philosophical realist"; document.write("strlastIndexOf.lastIndexOf(ein): " + strlastIndexOf.lastIndexOf("ein") + "
"); document.write("strlastIndexOf.lastIndexOf(philo): " + strlastIndexOf.lastIndexOf("philo") + "
"); document.write("strlastIndexOf.lastIndexOf(l): " + strlastIndexOf.lastIndexOf("l") + "
"); // match() Searches for a match between a regular expression and a string, and returns the matches var strMatch = "Einstein defined Morality as giving much and taking very little"; var pattern = /ein/gi; document.write("strMatch.match(pattern): " + strMatch.match(pattern) + "
"); // replace() Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring // SYNTAX: string.Replace("regexp or oldstring", "newstring") var strReplace = "Ptolemy thought the earth was flat"; document.write("strReplace.replace(Ptolemy, The Ancients)" + strReplace.replace("Ptolemy", "The Ancients") + "
"); // search() Searches for a match between a regular expression and a string, and returns the position of the match // SYNTAX: string.search("regexp or string") var strSearch = "Copernicus said the sun is in the center."; document.write("strSearch.search(sun): " + strSearch.search("sun") + "
"); // slice() Extracts a part of a string and returns a new string // SYNTAX: string.slice(beginIndex,endIndex) - endIndex is optional var strSlice = "Copernicus said the sun is in the center."; document.write("strSlice.slice(0): " + strSlice.slice(0) + "
"); var strSliceA = "Copernicus said the sun is in the center."; document.write("strSliceA.slice(7,20): " + strSliceA.slice(7,20) + "
"); // split() Splits a string into an array of substrings // SYNTAX: string.split(separator,limit) // separator is the character(s) that define where split occurs // limit is optional and defines max number of strings to return var strSplit = "Copernicus said the sun is in the center."; document.write("strSplit.split( ): " + strSplit.split(" ") + "
"); var strSplitA = "Copernicus said the sun is in the center."; document.write("strSplitA.split( ,3): " + strSplitA.split(" ",3) + "
"); // substr() Extracts the characters from a string, beginning at a specified start position, and through the specified number of character // SYNTAX: string.substr(startIndex,EndIndex) - EndIndex is optional var strSubstr = "Newton believed in action at a distance."; document.write("strSubstr.substr(0,15): " + strSubstr.substr(0, 15) + "
"); // substring() Extracts the characters from a string, between two specified indices // SYNTAX: string.substr(fromIndex,ToIndex) - toIndex is optional and is not included // in the returned string var strSubstring = "Newton believed in action at a distance."; document.write("strSubstring.substring(0,15): " + strSubstring.substring(0, 15) + "
"); // toLowerCase() Converts a string to lowercase letters var strToLowerCase = "Einstein believed space and time are not rigid and linear."; document.write("strToLowerCase.toLowerCase(): " + strToLowerCase.toLowerCase() + "
"); // toUpperCase() Converts a string to Uppercase letters var strToUpperCase = "Einstein believed space and time are not rigid and linear."; document.write("strToUpperCase.toUpperCase(): " + strToUpperCase.toUpperCase() + "
"); // valueOf() Returns the primitive value of a String object var strValueOf = "Einstein believed space and time are not rigid and linear."; document.write("strValueOf.valueOf(): " + strValueOf.valueOf() + "
");