Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

10
Computation as an Computation as an Expressive Medium Expressive Medium Lab 6: Strings and Text Lab 6: Strings and Text Manipulation Manipulation Annie Lausier Annie Lausier

Transcript of Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

Page 1: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

Computation as an Computation as an Expressive MediumExpressive Medium

Lab 6: Strings and Text Lab 6: Strings and Text ManipulationManipulation

Annie LausierAnnie Lausier

Page 2: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

……the JAWS of a MIGHTY the JAWS of a MIGHTY WHALE!WHALE!

Our good pal, Mr. StringOur good pal, Mr. String His amazing properties!His amazing properties! Swell methods that make him Swell methods that make him ginchy!ginchy!

Text and fonts… Text and fonts…

Page 3: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

And now, hailing from And now, hailing from Halifax…Halifax…

You may recall when You may recall when we talked about we talked about typestypes PrimitivesPrimitives

int, float, byteint, float, byte booleanboolean charchar

Objects (composites)Objects (composites) ArrayArray ArrayListArrayList PImagePImage (any object you (any object you create)create)

and…and…

String

???"OMGLOL this is

teh awesomz0rz!!!!

11!;@!"

"Thank you! Thank you so much! I love you all!"

"No, really,

you're far too kind."

Page 4: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

String detailsString details

A string is A string is almostalmost like an array of like an array of charcharss charchar someletter = someletter = ''bb'';; StringString somewords = somewords = ""Howdy-do, mr. jones?Howdy-do, mr. jones?"";; Note the use of Note the use of double-quotesdouble-quotes (vs.(vs. apostrophes apostrophes))

LikeLike the the objects we've createdobjects we've created with with classes, it has several classes, it has several methodsmethods, too…, too…

Page 5: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

String methodsString methods

From From http://processing.org/reference/String.htmlhttp://processing.org/reference/String.html

length()length() returns the size of the String (number of returns the size of the String (number of letters)letters)

charAt(charAt(numbernumber)) returns the returns the charchar at an at an indexindex numbernumber

toUpperCase()toUpperCase() and and toLowerCase()toLowerCase() returns a copy of the String in returns a copy of the String in UPPERCASEUPPERCASE or or lowercaselowercase respectively. respectively.

substring(substring(beginIndexbeginIndex, , endIndexendIndex)) returns returns a portiona portion of the String from of the String from beginIndexbeginIndex

String howdy = "Hello!"; String expletive = howdy.substring(0,3);

Page 6: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

String concatenationString concatenation

ConcatenationConcatenation is just a is just a fancy wordfancy word for for slapping together to make one!slapping together to make one!

With With StringStrings, this is done using the s, this is done using the ++ symbol symbol

So, if you have:So, if you have:

You'll get out: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.

Page 7: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

MORE String MORE String concatenationconcatenation

BUT WAIT! BUT WAIT! You can also add in You can also add in numbers, too! numbers, too! Great Holy Scott Great Holy Scott Pilgrim!Pilgrim!

There is also a function called There is also a function called nf()nf() which can format your numbers (it which can format your numbers (it stands for number format)stands for number format)

It has siblings! It has siblings! nfs(); nfp(); nfs(); nfp(); nfc();nfc(); Consult the reference. 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."

Page 8: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

Strings and ArraysStrings and Arrays

Did you know that you can take Did you know that you can take an Array of Strings and an Array of Strings and joinjoin it it into one String?into one String?

Did you also know that you can Did you also know that you can splitsplit a String into an Array? 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…" }

Page 9: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

Special charactersSpecial characters

We talked about splitting strings We talked about splitting strings based on spaces (based on spaces (" "" "), but remember ), but remember we can also use tabs and new line we can also use tabs and new line characters (the carriage return)characters (the carriage return) tab: "tab: "\t\t"" new line: "new line: "\n\n""

other other escape charactersescape characters include include ""\\\\" " ""\"\"""

String twolines = "I am on one line.String twolines = "I am on one line.\n\n I am I am \t\ton another."on another."

I am on one line.I am on one line.I am on I am on another.another.

( ( \\ tells the computer to tells the computer to look to the look to the next characternext character to figure out what to do to figure out what to do that's special.)that's special.)

Page 10: Computation as an Expressive Medium Lab 6: Strings and Text Manipulation Annie Lausier.

RecapRecap

We've talked about We've talked about Strings…Strings…

Methods and Methods and concatenationconcatenation

Strings and ArraysStrings and Arrays FontsFonts Reading from text Reading from text filesfiles

Let's look at Let's look at cooooooode!cooooooode!

(No funny today, either. Sorry.)

mouth

eye eyeeyebro

w

long, spindly, twig-like

body

straight arm of medium

length

short, puny

arm