Title: Computation as an Expressive Medium
1Computation as an Expressive Medium
- Lab 6 Strings and Text Manipulation
- Jason Alderman
2the JAWS of a MIGHTY WHALE!
- Our good pal, Mr. String
- His amazing properties!
- Swell methods that make him ginchy!
- Text and fonts
3And now, hailing from Halifax
- You may recall when we talked about types
- Primitives
- int, float, byte
- boolean
- char
- Objects (composites)
- Array
- ArrayList
- PImage
- (any object you create)
- and
"OMGLOL this is teh awesomz0rz!!!!11!_at_!"
String
"Thank you! Thank you so much! I love you all!"
"No, really, you're far too kind."
4String details
- A string is almost like an array of chars
- char someletter 'b'
- String somewords "Howdy-do, mr. jones?"
- Note the use of double-quotes (vs. apostrophes)
- Like the objects we've created with classes, it
has several methods, too
5String methods
- From http//processing.org/reference/String.html
- length()
- returns the size of the String (number of
letters) - charAt(number)
- returns the char at an index number
- toUpperCase() and toLowerCase()
- returns a copy of the String in UPPERCASE or
lowercase respectively. - substring(beginIndex, endIndex)
- returns a portion of the String from beginIndex
String howdy "Hello!" String
expletive howdy.substring(0,3)
6String concatenation
- Concatenation is just a fancy word for slapping
together to making one! - With Strings, this is done using the symbol
- So, if you have
- You'll get out
String s1 "She is the " String s2
"programmer." String sentence s1 "awesomest "
s2
println(sentence) // sentence "She is the
awesomest programmer." // outputs She is the
awesomest programmer.
7MORE String concatenation
- BUT WAIT! You can also add in numbers, too!
Great Holy Scott Pilgrim! - There is also a function called nf() which can
format your numbers (it stands for number format) - It has siblings! nfs() nfp() nfc() Consult
the reference.
String anothersentence s1 "" 2 " "
s2// "She is the 2 programmer."
anothersentence s1 nf(7,3) " " s2//
nf( integer, number of digits )// "She is the
007 programmer." anothersentence s1
nf(3.14159,3,2) " " s2// nf( float, digits
before decimal, digits after decimal )// "She is
the 003.14 programmer."
8Strings and Arrays
- Did you know that you can take an Array of
Strings and join it into one String? - Did you also know that you can split a String
into an Array?
String a "One", "string", "to", "rule",
"them", "all" String tolkien join(a, "
")// tolkien "One string to rule them all"
String b "Another string to bind
them" String tolkien2 split(b, " ")//
tolkien2 "Another", "string", "to", "bind",
"them"
9Special characters
- We talked about splitting strings based on spaces
(" "), but remember we can also use tabs and new
line characters (the carriage return) - tab "\t"
- new line "\n"
- other escape characters include "\\" "\""
( \ tells the computer to look to the next
character to figure out what to do that's
special.)
String twolines "I am on one line.\n I am \ton
another."I am on one line.I am on another.
10Recap
- We've talked about Strings
- Methods and concatenation
- Strings and Arrays
- Fonts
- Reading from text files
- Let's look at cooooooode!
(No funny today, either. Sorry.)
eyebrow
eye
eye
mouth
short, puny arm
straight arm of medium length
long, spindly, twig-like body