Goals

  • Understanding Strings
    • Treating Strings as Objects
    • Declaring and Initializing
    • String Operations

Resources

http://processing.org/learning/basics/charactersstrings.html
http://processing.org/learning/basics/letters.html
http://processing.org/learning/basics/words.html

"Words form the thread on which we string our experiences." ~ Aldous Huxley

String Objects

A string is nothing more than a "string" of characters. Since the use of strings is so essential in programming we often refer to strings as a datatype, however at their core they are simply a special object in processing and java. Let's take a look at how strings are declared and initialized.

String hello; declares a variable of type String named hello
hello = "Hello"; initializes the String hello to equal "Hello"
String hello = "Hello"; declaration and initialization in one line

Strings must be initialized to double quoted text (referred to as a string literal).
In the above example hello is the string variable and "Hello" is the string literal.

Strings are declared and initialized just like primitive data types.
String variables must represent a double quoted string literal.

Concatenation

One of the nice things about strings are their features. Concatenation is just a fancy word for string addition, which means we can add one string to the beginning or end of another. Let's take a look.

String hello = "Hello ";
String user = "Matt";
String helloUser = hello + user; helloUser -> "Hello Matt"
String byeUser = "Bye " + user; byeUser -> "Bye Matt"

Notice the spaces at the end of the hello string variable and goodbye string literal. When concatenating strings there is no additional space added which means it is important to think ahead about how your strings will be used.

Char At and Length

One simple method of the string object is the ability to pic out a character at a specific position. The other is a method to retrieve the length.

String hello = "Hello";
char theLetterL = hello.charAt(2);
char theLetterO = hello.charAt(4);
char theLetterO = hello.charAt(hello.length()-1); from the back

Notice how we used the length - 1 to get the last character? This is because strings, like arrays, are 0 indexed. That means that when grabbing characters and specifying indices we count the first character as 0. So the string variable hello has a length of 5, but the position of the "o" character at the end is actually 4.

Substring

Another feature of strings is the ability to take out parts you may need. For instance, imagine we needed the username from the previous example, but we only had the string variable helloUser?

String helloUser = hello + user; helloUser -> "Hello Matt"
String user = helloUser.substring(6, 10); user -> "Matt"

Notice the spaces at the end of the hello string variable and goodbye string literal. When concatenating strings there is no additional space added which means it is important to think ahead about how your strings will be used.

Uppercase and Lowercase

What if we had a message and needed to shout or whisper it?

String hello = "Hello";
String bigHello = hello.toUpperCase(); bigHello -> "HELLO"
String littleHello = bigHello.toLowerCase(); littleHello -> "hello"

Equality

We might want to check if two strings are equal to eachother. We can also combine this method with a toLowerCase to make the check case insensitive.

String user1 = "matt";
String user2 = "Matt";
user1.equals(user2); false
user1.toLowerCase().equals(user2.toLowerCase()); true

String concatenation will not add any spaces, plan ahead.
Strings are zero indexed. The character positions start at 0.
The last position in a string is always length() - 1.
When using string1.equals(string2) remember this check is case sensitive.