trim() removes whitespace characters from the beginning and end of a String. In addition to standard whitespace characters such as space, carriage return, and tab, this function also removes the Unicode "nbsp" character.
// Trim example size(500,220, P3D); background(20); stroke(128, 128, 128); fill(64); rect(1, 1, width-3, height-3); String s1 = "normal string"; String s2 = " extra leading spaces"; String s3 = "extra trailing spaces "; String s4 = " both leading and trailing "; String[] a = { " inconsistent ", " spacing", "ok", " tab at the end "}; String[] a2 = trim(a); PFont font; font = loadFont("Arial"); textFont(font, 14); fill(32, 255, 0); text("Strings", 5, 20); text("String Arrays", 5, 120); fill(255, 255, 255); text("s1: |" + s1 + "|", 15, 40); text("after: |" + trim(s1) + "|", 240, 40); text("s2: |" + s2 + "|", 15, 60); text("after: |" + trim(s2) + "|", 240, 60); text("s3: |" + s3 + "|", 15, 80); text("after: |" + trim(s3) + "|", 240, 80); text("s4: |" + s4 + "|", 15, 100); text("after: |" + trim(s4) + "|", 240, 100); for(int i=0; i<a2.length; i++){ text("a[" + i + "]: |" + a[i] + "|", 15, 140+(i*20)); text("after: |" + a2[i] + "|", 240, 140+(i*20)); }