AdvWeb-1/14 DePaul University SNL 262 Advanced Web Page Design Chapt 6 - Strings & Arrays Instructor: David A. Lash
AdvWeb-2/14 Chapt 6 - String Types u You can store numbers or characters into variables. u For example, x=12; or x="Dave"; u JavaScript allows 2 different ways to assign a set of characters into a variable: x = "Dave"; or x = new String("Dave"); u These 2 assignments are equal. The 2nd explicitly creates a new object called string and assigns into it the initial value called Dave. u JavaScript allows either assignment. The second is much closer to the Java string definition and assignment.
AdvWeb-3/14 Manipulating Strings u Simple assignment statements can be used to alter string values in JavaScript. u The "+" sign is used to concatenate two strings together: x = new String("Dave"); y = "Tom"; x = y; y = y + x; document.write("x=", x, " " ); document.write("y=" + y + " ");
AdvWeb-4/14 Using Strings u A string is an object u Objects have properties and behaviors u Properties are attributes you can look at - Access properties via object_name.property. – For example, strings have length test=“A String”; document.write(test.length); u Behaviors are functions for the object - Access properties via object_name.property. – For example, functions available for u toUpperCase() - converts string to all upper case u toLowerCase() - converts string to all lower case test=“A String”; document.write(test.toLowerCase());
AdvWeb-5/14 Using Strings - Substrings u Here is another behavior for string objects – text.substring(3,6); – returns the portion of string text starting at 3 and ending at 6 – So text=“Apple” text.substring(1,2); returns “A”
AdvWeb-6/14 Strings Example: u A more complete example... Example String x = window.prompt(“Enter a String”); document.write(x.length, “ ”); document.write(x.toUpperCase(), “ ”); document.write(x.toLowerCase(), “ ”); See: stringtoUpper.html
AdvWeb-7/14 Another String Function u Here is yet another behavior – location = test.indexOf( “Dave”); – searches for the string “Dave” in the string test and returns the positional value value if it is in there. – Returns -1 if not there. Example String x = window.prompt("Enter a String"); document.write(x.indexOf("Dave"), " "); document.write("String was=", x); See: /examples/stringtoIndex.htmlexamples/stringtoIndex.html
AdvWeb-8/14 Introduction To Arrays u Arrays are a way to group sets of data items as a single unit: u First you declare the size of array (or number of items you want to group together) scores = new Array(30); scores[0] = 39; scores[1] = 40; scores[2] = 55; scores[3] = 99; average=(scores[0]+scores[1]+scores[3]+scores[2])/4; document.write( “average=“, average ); u Arrays have many advantages that we will look at some now (and others when we look at looping). - e.g., x=scores[1+1]; Variable name Subscript
AdvWeb-9/14 Creating Arrays u var a new Array(); – creates an empty array with no elements u var a new Array(5, 4, 3, 2, 1); – creates an array with 5 elements and initializes them. u var a = new Array(10); – creates an array of 10 elements each set with the undefined value and sets length property to 10.
AdvWeb-10/14 An Array Example Example String Sample Array ; numb = new Array(5); numb[0]=window.prompt("Enter Number 0"); numb[1]=window.prompt("Enter Number 1"); numb[2]=window.prompt("Enter Number 2"); numb[3]=window.prompt("Enter Number 3"); numb[4]=numb[0]*1+1; index = window.prompt("Enter an index number from 0-4"); if ( index = 0 ) { document.write("Your index was " + index + " "); document.write("The Array element was " + numb[index] + " "); } else document.write("I said a number between 0-5 " ); See:: examples/ 5_arrayexample1.html
AdvWeb-11/14 Some array procedures & methods u length var a = new Array(10); x=a.length; u sort() var a = new Array(1, 33, 12, 4, 0); a.sort(); u reverse() var a = new Array(1, 44, 2, 4); a.reverse();
AdvWeb-12/14 Another Array Example Example String Sample Array ; var a = new Array(1, 33, 12, 4, 0); for (i=0; i<a.length; i++ ) { document.write("presort = item number" + i + "=" + a[i] + " "); } document.write(" "); a.reverse(); for (i=0; i<a.length; i++ ) { document.write("reverse item number" + i + "=" + a[i] + " "); } document.write(" "); a.sort( ); for (i=0; i<a.length; i++ ) { document.write("post sort item number" + i + "=" + a[i] + " "); } See:: examples/ 5_arrayexample2.html
AdvWeb-13/14 String Arrays u Split: splits a string from 1 string into an array of different parts: – testname=“David A. Lash”; – parts=testname.split(“ “); u Example: Example String Test String Split ; testname = "David A. Lash"; parts = testname.split(" "); document.write( parts[2], ", ", parts[0], " ", parts[1], " " ); See: StringS plit.html StringS plit.html
AdvWeb-14/14 Scrolling Example Scrolling Message Example var msg = "This is an example of a scrolling message. Isn't it exciting?"; var spacer = "......"; var pos = 0; function ScrollMessage() { window.status = msg.substring(pos, msg.length) + spacer + msg.substring(0, pos); pos++; if (pos > msg.length) pos = 0; window.setTimeout("ScrollMessage()",200); } ScrollMessage(); Scrolling Message Example Look at the status line at the bottom of this page. (Don't watch it too long, as it may be hypnotic.) See: croll.html