1 / 18

Text

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 *

kasi
Download Presentation

Text

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. Text Text is data too!

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

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

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

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

  6. 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]

  7. The length of a string • The length of a string is obtained via • SOME_TEXT.length • Examples • “popcorn”.length • “Hat”.length • “3256”.length

  8. 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”

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

  10. 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)

  11. 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(“@”)

  12. 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"

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

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

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

  16. 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)

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

  18. Case Study : Date • Write code to convert from European date to American date • edate← readEuropeanDateFromUser() • day← ??? • month ← ??? • year ← ??? • americanDate← ???

More Related