Processing+-+Strings

Strings are one of the main data types that we learned about earlier in the course.

They are useful because we can store characters or words in them.

The characters we store are called ASCII characters.

What does ASCII stand for?

How are ASCII characters stored in the computer?

You can imagine a String as a series of characters stored together. Each character has an **index**, which is the position of the character in the String. Index numbers start at 0.

Example: We can find out how long a String is by calling the name of the String variable with .length as in: code format="java" String greeting = "Hello!"; println(greeting.length); //This prints out: code
 * Character: || H || e || l || l || o || ! ||
 * Index: || 0 || 1 || 2 || 3 || 4 || 5 ||

We can turn any String into an "all-caps" String by using .toUpperCase as in: code format="java" String greeting = "good day"; println(greeting.toUpperCase); //should print out: code and there is a similar method .toLowerCase.

Try It: 1. Make a greeting like those featured above. Use .toLowerCase to print it out in lower case letters.

We can grab little bits of Strings by using .substring(start, finish) as in: code format="java" String fruit = "Banana"; println(fruit.substring(2, 4)); //should print out "  "- prints from indexes start to (finish - 1)

code

and we can get one character at a time by using .charAt(index) as in: code format="java" String food = "pizza"; println(food.charAt(3)); //prints out ______________ from "pizza" - the character at index 3. code
 * Note:* .charAt will return data of type *char* which holds only one character at a time. You may run into problems if you try to use char variables as Strings. char joins int, boolean, and float as a core collection of *primitive* data types.

If we want to find out where (or if!) a character is in a String at all, we can use .indexOf(character) as in: code format="java" String joke="ha ha"; println(joke.indexOf('a')); //this prints out: - for the first 'a' in "ha ha" println(joke.indexOf('q')); //what does this print out? code

Note that .indexOf will take Strings or char variables, or Strings or chars in quotes. You can also put Strings with several characters into charAt, and find out the first location of those Strings. We call these pieces of Strings *substrings.* (Hence, the name from above.)

code format="java" String game="Monkey in the middle"; println(game.indexOf("in")); //should print out: println(game.indexOf("back"); //should print out: code

Finally, we can join several Strings together using *concatenation*. That's just a fancy name for joining Strings. We've done this already - the easiest way is to use the + symbol as in: code format="java" String test1 = "Jolly"; String test2 = "Green"; String test3 = "Giant"; String test4 = test1 + test2 + test3; println(test4); //What will this print out? code

Try it: 1. Write a program to scramble your name. Here's how it works: Take the two letters of your first name and switch them with the first two letters of your last name. Then add "aaa" to the beginning of your new first name and "ooo" to the end of your last name. Make the final name scroll across the screen. Can you make it scroll repeatedly? 2. Write a program to determine if a String is a "valid" email address (a "valid" email address has an "@" sign in it). You can don't need to make this print out to the screen. If it is valid, it should print out "_ is a valid email address" (where is the email address). If it is not valid, it should print out " __ is not a valid email address." Here's some starter code: code format="java" void setup { String testEmail = "yourmom@email.com"; //erase this comment and put your code here } code

There are more String methods that are available to you, you can found out more about them at: @http://processing.org/reference/String.htmlor @http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html