Saint Louis University |
Computer Science 144
|
Dept. of Math & Computer Science |
Note: This document is intended only to be a very brief outline of the content we covered. Far more thorough discussion and documentation can be found in the other reference materials linked on the schedule for today.
Overview: Our general theme for the day is how text is internally represented and manipulated within a Processing program, how it can be loaded from an external file, and how it can be outwardly rendered to the screen.
char data type: To represent a single character of text, Processing uses a built-in primitive type named char. Character constants are indicated using single quotes (e.g., 'a').
String data type:
To represent an arbitrary length sequence of characters, Processings
uses the String data type. Note that this data type must be
capitalized (as String is formally a class
in Processing and Java). String literals are expressed using
double-quote symbols, and strings can have zero, one, or more
characters
Strings support a variety of useful functions, including the following. For the sake of example, assuming that variable sample references a String.
sample.length()
Returns the number of characters within the string. Note well
that parentheses are required to call this function (unlike the
syntax a.length used for an array a).
sample.charAt(j)
returns the single character at index j of the string;
note that strings are indexed as with arrays, with the first
character having index 0.
For example,
sample.substring(j, k)
Returns a string including characters starting at
index j and going up to but not including
index k. Note well the asymmetry of the
convention; one advantage of this is that you find that
substring(i,j) + substring(j,k) equals
substring(i,k), as the character at
index j only occurs once.
For example,
sample.indexOf(pattern)
Returns the first index at which the pattern string can
be found as a substring of the full string.
For example,
sample.toLowerCase()
Returns a new string with all characters of original turned to
lower case.
For example,
sample.toUpperCase()
Returns a new string with all characters of original turned to
upper case.
For example,
sample.equals(other)
Returns true if the sample string is
character-for-character equivalent to the parameter string, and
false otherwise.
There is a very important (but subtle) issue in Processing (and Java), which requires use of the equals function for testing such equality. You should generally avoid using the == operator when working with strings.
Trimming Strings
Processing supports a top-level function trim that can be used to produce a new string that has extraneous whitespace removed from both ends of an original string (but not the middle). For example,
trim(" this is a test ")returns the string
Splitting and Joining Strings
Processing supports several top-level functions that can be used to break one string into many strings using a given delimeter, or in taking many strings and joining them into a single string using a given delimeter between the pieces.
split: The general syntax split(original, delim) can be used where original is a string and delim is a single character; it returns an array of strings. For example
split("alice/bob/carol", '/')returns an array of length 3 with contents
splitTokens: The general syntax splitTokens(original) can be used to split the original string using any form of possibly repeated whitespace to separate the string.
The form syntax splitTokens(original, delimeters) uses a string as the second parameter and allows for any characters of the delimeter string to be used when splitting the original.
join: The general syntax
Over time, we will explore several existing functions that allow us to load data from within a file stored on your computer, or loaded from the web via a URL. The first such function we examine is loadStrings(). It accepts a parameter which is either the name of a file on your computer or a URL, and it returns an array of String objects representing the individual lines of the given file.