This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Name

String

Examples
String str1 = "CCCP";
char data[] = {'C', 'C', 'C', 'P'};
String str2 = new String(data);
println(str1);  // Prints "CCCP" to the console
println(str2);  // Prints "CCCP" to the console

// Comparing String objects, see reference below.
String p = "potato";
if (p == "potato") {
  println("p == potato, yep.");  // This will not print
} 
// The correct way to compare two Strings
if (p.equals("potato")) {
  println("Yes, the contents of p and potato are the same.");
}

// Use a backslash to include quotes in a String
String quoted = "This one has \"quotes\"";
println(quoted);  // This one has "quotes"
Description A string is a sequence of characters. The class String includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for converting an entire string uppercase and lowercase. Strings are always defined inside double quotes ("Abc"), and characters are always defined inside single quotes ('A').

To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)

Because a String is defined between double quotation marks, to include such marks within the String itself you must use the \ (backslash) character. (See the third example above.) This is known as an escape sequence. Other escape sequences include \t for the tab character and \n for new line. Because backslash is the escape character, to include a single backslash within a String, you must use two consecutive backslashes, as in: \\

There are more string methods than those linked from this page. Additional documentation is located online in the official Java documentation.
Methods
charAt() Returns the character at the specified index
equals() Compares a string to a specified object
indexOf() Returns the index value of the first occurrence of a substring within the input string
length() Returns the number of characters in the input string
substring() Returns a new string that is part of the input string
toLowerCase() Converts all the characters to lower case
toUpperCase() Converts all the characters to upper case
Constructor
String(data)
String(data, offset, length)
Parameters
data byte[] or char[]: either an array of bytes to be decoded into characters, or an array of characters to be combined into a string
offset int: index of the first character
length int: number of characters
Relatedchar
text()
Updated on January 21, 2019 10:05:16am EST

Creative Commons License