7 – Strings & Chars

A String (notice the capital “S”) is another variable type that holds type.
String theText = “joe is good”;
would put “joe is good” into the variable theText.

PFont font; 
String theText; 
 
void setup(){ 
  size(400, 200); 
  theText = "cheese is good"; 
  font = createFont("Helvetica", 24);
  textFont(font);
}
 
void draw(){
  background(200); 
  text(theText, 20, 90); 
}

A char is a single character, and you can think of a String as an array of chars. You use the term charAt() to get a char from a string. The line
char letter = theText.charAt(2);
in the code above this line would set the variable letter to “e” (don’t forget arrays start at 0!)
You can add to a string with the + symbol, but you cannot compare strings with the = sign, you have to use “equals”

PFont font; 
String theText, theText2; 
 
void setup() { 
  size(400, 200); 
  theText = "cheese is good"; 
  theText2 = "cheese is real good"; 
  font = createFont("Helvetica", 24);
  textFont(font);
}
 
void draw() {
  background(200); 
  if (theText.equals(theText2) == true) {
    println("it's the same yo!");
  }
  else {
    println("Not the same, fool!");
  }
}

Strings can also go into arrays.

Font font; 
String [] textList = new String [5]; 
int wordNum = 0; 
int wordX = 800; 
void setup() { 
  size(800, 100); 
  textList [0] = "stuff"; 
  textList [1] = "things"; 
  textList [2] = "objects"; 
  textList [3] = "junk"; 
  textList [4] = "detritus"; 
 
  font = createFont("Helvetica", 24);
  textFont(font);
}
void draw() {
  background(0); 
  wordX -= 10; //this takes 10 pixels off our word's X location every loop
  if (wordX < -20) {  //if word x location gets to < -20 
    wordX = width + 20; //make it  820 again
    wordNum++;  //and move onto next spot in the array
    fill(random(255), random(255), random(255)); //choose a new color
  }
  if (wordNum == 5) { //if we get to the end of our word array
    wordNum = 0; //go back to first position (index no. 0)
  }
  text(textList[wordNum], wordX, height/2); //draw text to screen
}

This sketch has a string called phrase that is catching keystrokes. Notice in this code we actual’y set the phrase to “” which is an empty string. For some reason it won’t work if we query a string that has nothing in it.

PFont font; 
String [] textList = new String [5]; 
String phrase = ""; 
int wordNum = 0; 
int wordX = 800; 
boolean checkPhrase; 
 
void setup() { 
  size(500, 200); 
  textList [0] = "stuff"; 
  textList [1] = "things"; 
  textList [2] = "objects"; 
  textList [3] = "junk"; 
  textList [4] = "detritus"; 
 
  font = createFont("Helvetica", 24);
  textFont(font);
}
void draw() {
  background(0); 
  fill(255); 
  text("what is in the drawer?", 30, 30); 
  text(phrase, 30, 60); 
  checkPhrase = false; 
  for (int i = 0; i < textList.length; i++) {
    if (phrase.equals(textList[i]) == true) {
      checkPhrase = true;
    }
  }
  if (checkPhrase == true) {
    text("you're right!", 200, 140);
  }
  else {
    text("you're not even close!", 200, 140);
  }
}
 
void keyReleased() {
  phrase += key;
}

This works well, but what about the delete key? Start by printing out the keyCode in the keyReleased area – this will get the code for return too. You’d think that deleting the string by one character would be easy, but it’s not. Here’s how you do it.

if (keyCode == 8 && phrase.length() > 0) { 
phrase = phrase.substring(0, (phrase.length() -1));
}

The first line says: “if the keyCode for the delete key is pressed and the String isn’t at 0 already, then …”
The second line says: “take your string and make another string that happens to be one character shorter than the current string, oh and name the new string the same thing as the old string.”

Exercise

Find or write a haiku poem.
Dynamically represent that poem on the screen.
Each line of the poem must be a separate line in an array.

 

More material.

See this very useful tutorial on writing and displaying strings to the screen – the tutorial here.