180 likes | 311 Views
Text. Text is data too!. Overview. Numbers are fairly 'easy' since you already know: What they are. For example, which of the following are numbers? Solo 35 Luke Skywalker True How they can be processed. For example, which of the following is a valid number-processing operation? AND *
E N D
Text Text is data too!
Overview • Numbers are fairly 'easy' since you already know: • What they are. For example, which of the following are numbers? • Solo • 35 • Luke Skywalker • True • How they can be processed. For example, which of the following is a valid number-processing operation? • AND • * • substring • Which of the following, then, is meaningful? • True * 35 • 35 AND 35 • 35 * 35 Image courtesy http://www.flickr.com/photos/shaunwong/2302453730/sizes/z/in/photostream/
Overview • Logical values are similar. You should already know • What they are. For example, which of the following is a logical value? • Solo • 35 • Luke Skywalker • True • How they can be processed. For example, which of the following is a valid logical operation? • AND • * • substring • Which of the following is meaningful? • True * True • True AND False http://www.flickr.com/photos/27271711@N04/3059387885/
Text as Data • Text is a little trickier since you may not realize • What text is • How text can be processed • In most programming languages, textual data is denoted by using double-quotes to surround the text • “Hello?” • “Computational Thinking” • “04/13/65” • “32”
Text (String) Basics • Characters in a string are indexed such that the first character is at index 0 • The length of a string is defined as the number of characters in the string. • The length will never be negative • The length may be 0 • Denoted as SOME_STRING.length • “popcorn”.length Indices Characters
String Indexing • We can identify an individual character using indexing. Indexing is denoted as square brackets. • Syntax • SOME_TEXT[INDEX] • Examples: • “popcorn”[3] • “Hello?”[5] • “Computer”[0] • “Hat”[1]
The length of a string • The length of a string is obtained via • SOME_TEXT.length • Examples • “popcorn”.length • “Hat”.length • “3256”.length
Concatenation • Two strings can be concatenated using the concatenation operator. Concatenation takes two strings and splices them together to form a third string. • Syntax SOME_STRING + OTHER_STRING • Examples • “pop” + “corn” • “hot” + “dog” • “happy ” + “trails”
Naming • Names can be associated with numbers • x ← 3 • y ← 4 • z ← x + y • Names can be associated with text • x ← “pop” • y ← “corn” • z ← x + y
Substring • Indexing obtains a single character of a string • Substring obtains a section of a string. A section is denoted by where the section starts and ends • Syntax • SOME_STRING.substring(start_index, stop_index) • start_index is inclusive • stop_index is non-inclusive • Examples • x ← “computational thinking” • y ← x.substring(3,6)
Searching (indexOf) • May want to find the location of a character • Syntax: • SOME_STRING.indexOf(OTHER_STRING) • Examples: • “popcorn”.indexOf(“c”) • “popcorn”.indexOf(“pc”) • “elvispresley@heartbreak.hotel.com”.indexOf(“@”)
String Comparison • Often want to know if two strings are the same • A user logs into Facebook by entering their password. Is the password that they entered the same password that is stored in the servers database? • Syntax • SOME_STRING == OTHER_STRING • This asks a True/False question. • "Cat" == "Cat" • "Mouse" == "House"
Examples • x ← “cat” • y ← “hat” • z← (x+y)[3] • z ← x[0]+y • z ←(x[0]+y).length • z ←(x + x).substring(2,5) • z ←(x+x+x).length • z ← x.indexOf(“a”)*3
Case Study : Emails • Email addresses have a structure • The domain part is also known as the host-site • The ‘extension’ is the last segment of the host-site
Case Study : Emails • Given an email address • Identify the user name • Identify the host-site • Identify the extension • Consider the following code • address ← “beatles@yellowsubmarine.edu” • username ← address.substring(0,7) • hostsite← address.substring(8,28) • extension ← address.substring(25, 28)
Case Study : Emails • Write a program that will identify the elements of any valid email address? • address ← readAddressFromUser() • username ← ??? • hostsite← ??? • extension ← ??? • address ← “beatles@yellowsubmarine.edu” • username ← address.substring(0,7) • hostsite← address.substring(8,28) • extension ← address.substring(25, 28)
Case Study : Dates • The European date format is essentially the reverse of the American date format. • American: MM/DD/YYYY • European: DD/MM/YYYY • We might want to allow a European to enter a date using the European format but then convert the date to an American format so that it can be stored in our server’s database
Case Study : Date • Write code to convert from European date to American date • edate← readEuropeanDateFromUser() • day← ??? • month ← ??? • year ← ??? • americanDate← ???