Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 144
Introduction to Computer Science: Multimedia

Michael Goldwasser

Spring 2015

Dept. of Math & Computer Science

Lecture Notes: Characters, Strings, and Text


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 (e.g., "Hello", "How are you?", "a", and "").

Strings support a variety of useful functions, including the following. For the sake of example, assuming that variable sample references a String.


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 "this    is   a test".

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 ["alice", "bob", "carol"]. The split method is often used with the delimeter being the space character, the comma character, or the newline character.

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 join(stringArray, separator) produces a single string which is formed by joining all strings in the given array, with the given string separator used between each neighboring pair.


Loading data from files

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.


Michael Goldwasser
CSCI 144, Spring 2015
Last modified: Tuesday, 24 March 2015
Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit