Name

trim()

Description

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" (U+00A0) character and the zero width no-break space (U+FEFF) character.

Examples

  • Table table;
    
    void setup() {
    
      table = new Table();
    
      table.addColumn("name");
      table.addColumn("type");
    
      TableRow newRow = table.addRow();
      newRow.setString("name", "   Lion");
      newRow.setString("type", "Mammal");
    
      newRow = table.addRow();
      newRow.setString("name", "Snake  ");
      newRow.setString("type", "Reptile");
    
      newRow = table.addRow();
      newRow.setString("name", "  Mosquito  ");
      newRow.setString("type", "Insect");
      
      println(table.getStringColumn("name"));
      
      table.trim();
      
      println(table.getStringColumn("name"));
    }
    
    // Sketch prints:
    // [0] "   Lion"
    // [1] "Snake  "
    // [2] "  Mosquito  "
    // [0] "Lion"
    // [1] "Snake"
    // [2] "Mosquito"
    
  • String s1 = "    Somerville MA ";
    println(s1);  // Prints "    Somerville MA "
    String s2 = trim(s1);
    println(s2);  // Prints "Somerville MA"
    
    String[] a1 = { " inconsistent ", " spacing" };  // Note spaces
    String[] a2 = trim(a1);
    printArray(a2);
    // Prints the following array contents to the console:
    // [0] "inconsistent"
    // [1] "spacing"
    

Syntax

  • trim(str)
  • trim(array)

Parameters

  • str(String)any string
  • array(String[])a String array

Return

  • String or String[]