String Variables
What is a String?
Strings are a special data type contained in the processing library. It is an OBJECT: larger than a primitive and contains its own functions.
Using a string:
println(name); // no quotes
String Functions:
Example:
words.length(); //returns the length of the string
string1.concat(string2); // attaches string2 on the end of string 1
string1.compareTo(string2); // compares the two strings
string1.toLowerCase(); // changes string 1 to lower case
Strings are a special data type contained in the processing library. It is an OBJECT: larger than a primitive and contains its own functions.
- A string is an array of characters stored in a single object. When we give a string a value, it is used as a primitive with one difference. It is immutable – This means it cannot be changed. If you want to change the value of a string; copy the old contents with the new to make the changes.
- Because strings are used so often in coding, processing makes them as simple to use as possible.
Using a string:
println(name); // no quotes
String Functions:
- Since strings are OBJECTS they contain their own functions. These functions define the behaviour and attributes. They also let us do interesting operations.
Example:
- We have a string named words. We access the function by putting a dot after its name, then write the name of the method and any parameters needed by the function
words.length(); //returns the length of the string
string1.concat(string2); // attaches string2 on the end of string 1
string1.compareTo(string2); // compares the two strings
string1.toLowerCase(); // changes string 1 to lower case
String Methods
Start with a string “Processing is Great Fun!”
Println the string all uppercase, lowercase, and the length using string methods. Try some of the other ones
Println the string all uppercase, lowercase, and the length using string methods. Try some of the other ones