http://processing.org/learning/basics/charactersstrings.html
http://processing.org/learning/basics/letters.html
http://processing.org/learning/basics/words.html
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.
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.
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.
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.
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"
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