Related Articles

Related Articles

p5.Table removeTokens() Method
  • Last Updated : 10 Jul, 2020

The removeTokens() method of p5.Table in p5.js is used to remove all the specified characters from values in the table. A specific column can be specified for removing the token from only that column. However, if no column is specified then the values of all the columns and rows in the table are processed.

Syntax:

removeTokens( chars, [column] )

Parameters: This function accepts two parameters as mentioned above and described below:

  • chars: It is a String that specifies all the characters that have to be removed.
  • column: It is a String or integer that specifies the column name or ID of the column to be trimmed. It is an optional parameter.

The example below illustrates the removeTokens() method in p5.js.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  tokensInput = createInput();
  tokensInput.position(30, 40)
  
  trimBtn =
    createButton("Remove specified tokens");
  trimBtn.position(30, 80);
  trimBtn.mouseClicked(cleanTableData);
  
  // Create the table
  table = new p5.Table();
  
  // Add two columns
  table.addColumn("subject");
  table.addColumn("performance");
  
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("subject",
                   "----Maths---");
  newRow.setString("performance",
                   "====Good===");
  
  newRow = table.addRow();
  newRow.setString("subject",
                   "   English");
  newRow.setString("performance",
                   "__-Excellent--");
  
  newRow = table.addRow();
  newRow.setString("subject",
                   "Science");
  newRow.setString("performance",
                   ",,, ;;OK;");
  
  showTable();
}
  
function cleanTableData() {
  let tokensToRemove = tokensInput.value();
  
  // Remove given tokens only from the
  // whole table
  table.removeTokens(tokensToRemove);
  
  // Redraw the table
  showTable();
}
  
function showTable() {
  clear();
  
  // Display the rows present in the table
  for (let r = 0; r < table.getRowCount(); r++)
    for (let c = 0; c < table.getColumnCount(); c++)
      text(table.getString(r, c),
           20 + c * 100,
           140 + r * 20);
  
      text("Enter the tokens that have to be" +
           " removed from the table values"
           20, 20);
}

chevron_right


Output:
removeToken-ex1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  tokensInput = createInput();
  tokensInput.position(30, 40)
  
  trimBtn =
    createButton("Remove specified tokens");
  trimBtn.position(30, 80);
  trimBtn.mouseClicked(cleanTableData);
  
  // Create the table
  table = new p5.Table();
  
  // Add two columns
  table.addColumn("subject");
  table.addColumn("performance");
  
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("subject",
                   "----Maths---");
  newRow.setString("performance",
                   "-----Good===");
  
  newRow = table.addRow();
  newRow.setString("subject",
                   "-----English---");
  newRow.setString("performance",
                   "__-Excellent--");
  
  newRow = table.addRow();
  newRow.setString("subject",
                   "-Science---");
  newRow.setString("performance",
                   ",,, ;OK;");
  
  showTable();
}
  
function cleanTableData() {
  let tokensToRemove = tokensInput.value();
  
  // Remove given tokens only from the
  // 'name' column
  table.removeTokens(tokensToRemove,
                     'subject');
  
  // Redraw the table
  showTable();
}
  
function showTable() {
  clear();
  
  // Display the rows present in the table
  for (let r = 0; r < table.getRowCount(); r++)
    for (let c = 0; c < table.getColumnCount(); c++)
      text(table.getString(r, c),
           20 + c * 100,
           140 + r * 20);
  
      text("Enter the tokens that have to be"
           " removed from the table values",
           20, 20);
}

chevron_right


Output:
removeToken-ex2

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5.Table/removeTokens

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :