How to truncate, limit, clamp, constrain, shorten, etc ... a string?

I have a better feeling for how to limit the run on numbers in a long decimal using the nf() function in Processing.

However, how do you do this with a string?

I see no alternative so maybe you can still use nf()?

For instance I have a text input field and I want to limit the number of characters that can be listed. Such as only 10 characters, etc.

Thank you!

Someting like this?

import static javax.swing.JOptionPane.*; 

String words = "";

void setup() {
  size(640, 360);

void draw() {
  text("Type a string. (Only 10 characters allowed!)", 50, 90);
  text(words, 50, 120, 540, 300);
  if (words.length() > 10) { 
    showMessageDialog(frame, "Reached 10 characters limit");
    words = words.substring(0, words.length() - 1);

void keyTyped() {
  if ((key >= 'A' && key <= 'z') || key == ' ') {
    words = words + key;

Wow that is amazing nice work! That dialogue box is super awesome too I had no idea that was an option.

I will definitely play around with this and see what I can learn.

Thank you very much!!