Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

String Manipulation and Arrays in JavaScript: Lecture Notes for CSCI 100, Fall 2008, Slides of Artificial Intelligence

Lecture notes for csci 100, fall 2008, covering string manipulation using common methods like charat, substring, search, and concatenation. The notes also introduce javascript arrays as objects, their access and assignment, and the use of the split method for deriving acronyms and processing sequences of numbers.

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banani
banani 🇮🇳

4.3

(3)

91 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCI 100
Think Like Computers
Lecture 21
Fall 2008
Last Time …
While-loop
Loops with counters
Priming the while-loop
String as objects
Object oriented programming
Properties
Methods
String Manipulation Page Common String Methods
useful methods exist that allow progr ammers to access
and manipulate individual components of a string
components are identifiable via indices, or numbers that
correspond to the order in which individual characters occur in a
string
indices are assigned in ascending order from left to right, so that
the first character in the string is at index 0
the charAt method provides access to a single
character within the string
it takes an index as an input and returns the character at that
particular index
word = "foo";
ch = word.charAt(0); // ASSIGNS ch = "f"
Common String Methods
the substring method provides access to an
entire sequence of characters within the string
it takes two numbers as inputs, representing the
starting (inclusive) and ending (exclusive) indices of
the substring, and returns the substring
word = "foo";
sub = word.substring(1, 3);
// ASSIGNS sub = "oo"
String Access/Concatenation
recall: the concatenation operator (+) can join
strings together
assuming the variable word stores a string
value, what affect would the following
assignment have?
word = word.charAt(0) +
word.substring(1, word.length);
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download String Manipulation and Arrays in JavaScript: Lecture Notes for CSCI 100, Fall 2008 and more Slides Artificial Intelligence in PDF only on Docsity!

CSCI 100

Think Like Computers

Lecture 21

Fall 2008

Last Time …

• While-loop

Š Loops with counters

Š Priming the while-loop

• String as objects

Š Object oriented programming

Š Properties

Š Methods

String Manipulation Page Common String Methods

  • useful methods exist that allow programmers to access

and manipulate individual components of a string

Š components are identifiable via indices , or numbers that correspond to the order in which individual characters occur in a string Š indices are assigned in ascending order from left to right, so that the first character in the string is at index 0

  • the charAt method provides access to a single

character within the string

Š it takes an index as an input and returns the character at that particular index

word = "foo"; ch = word.charAt(0); // ASSIGNS ch = "f"

Common String Methods

• the substring method provides access to an

entire sequence of characters within the string

Š it takes two numbers as inputs, representing the

starting (inclusive) and ending (exclusive) indices of

the substring, and returns the substring

  • word = "foo"; sub = word.substring(1, 3); // ASSIGNS sub = "oo"

String Access/Concatenation

• recall: the concatenation operator (+) can join

strings together

• assuming the variable word stores a string

value, what affect would the following

assignment have?

word = word.charAt(0) +

word.substring(1, word.length);

String Access/Concatenation

• the following function takes a string as input and

uses string method calls to create (and return) a

capitalized version of that string

Searching Strings

  • the search method traverses a string in order to locate

a given character or substring

Š it takes a character or string as input and returns the index at which the character or string first occurs (or -1 if not found) str = "banana"; num1 = str.search("n"); // ASSIGNS num1 = 2 since the character // "n" first occurs at index 2 num2 = str.search("ana"); // ASSIGNS num2 = 1 since the string // "ana" first occurs at index 1 num3 = str.search("z"); // ASSIGNS num3 = -1 since the character // "z" does not occur anywhere

Searching Strings

• simple application: determine whether a string is

a single word or a phrase

Š if the string contains no spaces, the call

str.search(" ") will return -1, indicating that the

string value consists of a single word

Š if str.search(" ") returns a nonnegative value,

then the presence of spaces signifies a phrase

containing multiple words

General Searches

• there are times when you want to search for a

type of character, rather than a specific value

• example: converting a word into Pig Latin

Š if a word contains no vowels or begins with a vowel,

the characters “way” are appended to the end of the

word

nth Æ nthway apple Æ appleway

Š if a word begins with a consonant, its initial sequence

of consonants is shifted to the end of the word

followed by “ay”

banana Æ ananabay cherry Æ errychay

General Searches

in order to distinguish between these two cases,

must search for the first vowel

ƒ then, use the substring method to break the

string into parts and the + operator to put the

pieces back together (with "ay")

cherry Æ erry + ch + ay = errychay

General Searches

• rather than having to search for vowels

individually, an entire class of characters can be

specified using /[... ]/

Assigning Items in an Array

  • array items can be assigned values just like any variable Š suppose the array misc has been assigned to store the following

Š the assignment misc[0] = 1000 would store the value 1000 as the first item in the array, overwriting the value that was previously there

Assigning Items in an Array

• if the index in an assignment statement is

beyond the array’s current length, the array will

automatically expand to accommodate the new

item

Š the assignment misc[8] = "oops" would store

"oops" at index 8

From Strings to Arrays

• so far, our Web pages have handled each input

from the user as a single string

Š this approach is limiting since many programming

tasks involve separately processing an arbitrary

number of words or numbers entered by the user

From Strings to Arrays

Š recall the Pig Latin page Š we might want to generalize the page so that it translates entire phrases instead of just words Š that is, the user would enter an arbitrary sequence of words and each word would be translated Š this task would be very difficult using our current set of programming tools

String split Method

• JavaScript strings provide a method, split, for

easily accessing the components of a string

Š the only input required by the split method is a

character (or sequence of characters) that serves as

a delimiter for breaking apart the string

Š the split method separates the string into

component substrings at each occurrence of the

delimiter, and returns an array consisting of those

substrings

user = "Grace Murray Hopper"; arr1 = user.split(" "); // ASSIGNS arr1 to be the array // ["Grace", "Murray", "Hopper"]

String split Method

Š as was the case with strings, /[ … ]/ can be used to

specify groups of characters

Example: Generating Acronyms

• one interesting application of the split method

is in deriving acronyms

Š acronyms (terms made from the first letters of words

in a phrase) are especially popular in the computing

field

ƒ RAM – Random Access Memory ƒ GUI – Graphical User Interface ƒ WWW – World Wide Web

Example: Generating Acronyms

Example: Generating Acronyms

the Acronym function takes a phrase, such as "What you see is what you get", splits it into an array of individual words, then extracts the first letters to construct the acronym

Acronym Page

  • the Acronym function can be integrated into a Web page Š the user enters a phrase in a text area Š a button is defined to call the Acronym function with the text box contents as input Š the result returned by the function call is displayed in another text box

Arrays of Numbers

• some applications involve reading and

manipulating sequences of numbers

Š e.g., suppose we wanted to calculate the average of

some number of grades

Š we could enter the numbers in one big string,

separated by spaces, then

ƒ use the split function to separate out the individual numbers ƒ then, traverse the resulting array and calculate the sum and average of the numbers

Arrays of Numbers