240 likes | 332 Views
JavaScript Strings and Arrays. Kevin Harville. Strings and Arrays. Strings are indexed groups of CHARACTERS. Arrays are indexed groups of numbers. Therefore we can refer to a particular number or letter by referring to its index. Array Examples. I could have an array of images:
E N D
JavaScript Strings and Arrays Kevin Harville
Strings and Arrays • Strings are indexed groups of CHARACTERS. • Arrays are indexed groups of numbers. • Therefore we can refer to a particular number or letter by referring to its index.
Array Examples • I could have an array of images: • myImage[1] = “myDog.jpg” • myImage[2] = “myCat.jpg” • myImage[3] = “myFish.jpg” • Or an array of the students’ scores: • classScore[0] = 100 • classScore[1] = 95 • classScore[2] = 86
Strings and Arrays • Strings and Arrays are objects rather than variables. • Objects have properties and methods • myString = “Kevin” makes it look like a variable, but is actually a shortcut to make strings easy to use. • IMPORTANT: Strings and Arrays are indexed, or counted, starting at 0, not 1.
String Properties • myString.length
String Methods • String objects have several useful methods: • myString.toLowerCase(); • myString.toUpperCase(); • Others Follow…
String Methods: Substring • myString.substring(start, end+1); • “Kevin”.substring(0,2) returns “Ke” • “Kevin”.substring(1,3) returns “ev” • REMEMBER: ARRAYS START AT ZERO!
String Methods: charAt • charAt returns the value of the character at an index position: • “Kevin”.charAt(4) returns “n”why “n”?
String Methods: indexOf • “Kevin”.indexOf(“vi”) returns 2 • indexOf returns –1 if it is not found:Kevin.indexOf(“Joe”) returns –1 • You can specify the starting position:“Rin Tin Tin”. indexOf(“in”, 4) returns 5 • To find multiple occurrences repeat the process.
String Methods: lastIndexOf • Same as indexOf, but starts searching from the end.
String Methods: Split • This method creates an arraymyNewStrings = “Quick Brown Fox”.Split(“ “); • myNewStrings[0] = “Quick”myNewStrings[1] = “Brown”myNewStrings[2] = “Fox”
Some Other String Methods: • .replace (4.0 Browsers or higher) • HTML functionality, such as • .fontcolor • .fontsize • .strike • .etc.
Arrays • Again, an array is a group of indexed values. • For instance, instead of having individual class scores Score1, Score2, etc, you can have score[0], score[1]…score[30]. • So, What’s the difference?
Arrays • The difference is that the indexes can themselves be referred to by a variable! score[x] = 7 score[x + 1] = 4 score[y]= 9
Arrays • When we want a group of related data, we may declare an Array object: myArray = new Array(50) This would set up 50 elements, from 0 to 49. There is no 50th element.
Arrays • That indexing ability makes keeping track of related data very easy. Name[7] = “Fred” Lab1[7] = 5 Lab2[7] = 4 Midterm[7] = 12
Array Object: Sort Method • Using the sort method, you can sort a numeric or alphabetic array. • MyArray.sort()
Summary • Strings and arrays are used in most scripts • Learning them is necessary and a useful programming skill.