1 / 18

JavaScript Strings and Arrays

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:

gdurden
Télécharger la présentation

JavaScript Strings and Arrays

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. JavaScript Strings and Arrays Kevin Harville

  2. 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.

  3. 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

  4. 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.

  5. String Properties • myString.length

  6. String Methods • String objects have several useful methods: • myString.toLowerCase(); • myString.toUpperCase(); • Others Follow…

  7. 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!

  8. String Methods: charAt • charAt returns the value of the character at an index position: • “Kevin”.charAt(4) returns “n”why “n”?

  9. 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.

  10. String Methods: lastIndexOf • Same as indexOf, but starts searching from the end.

  11. String Methods: Split • This method creates an arraymyNewStrings = “Quick Brown Fox”.Split(“ “); • myNewStrings[0] = “Quick”myNewStrings[1] = “Brown”myNewStrings[2] = “Fox”

  12. Some Other String Methods: • .replace (4.0 Browsers or higher) • HTML functionality, such as • .fontcolor • .fontsize • .strike • .etc.

  13. 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?

  14. 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

  15. 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.

  16. Arrays • That indexing ability makes keeping track of related data very easy. Name[7] = “Fred” Lab1[7] = 5 Lab2[7] = 4 Midterm[7] = 12

  17. Array Object: Sort Method • Using the sort method, you can sort a numeric or alphabetic array. • MyArray.sort()

  18. Summary • Strings and arrays are used in most scripts • Learning them is necessary and a useful programming skill.

More Related