当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


p5.js trim()用法及代码示例


p5.j​​s中的trim()函数用于从输入String的开头和结尾删除空格字符和制表符。此函数还用于删除回车符和Unicode “nbsp”字符。

用法:

trim(Strings)

或者


trim(Array_of_Strings)

参数:此函数接受将要修剪的参数String或array_of_strings。

返回值:它返回修剪后的字符串。

以下程序说明了p5.js中的trim()函数。
示例1:本示例使用trim()函数从字符串的开头和结尾删除空格字符。

function setup() { 
  
    // Creating Canvas size 
    createCanvas(450, 120); 
} 
  
function draw() { 
  
    // Set the background color  
    background(220); 
  
    // Initializing the Strings and Array of strings 
    let String = " Geeks "; 
    let Array1 = [" 1 ", " 2 ", " 3 "]; 
    let Array2 = [" a ", " A ", " b ", " B "]; 
  
    // Calling to trim() function. 
    let A = trim(String); 
    let B = trim(Array1); 
    let C = trim(Array2); 
  
    // Set the size of text  
    textSize(16); 
  
    // Set the text color  
    fill(color('red')); 
  
    // Getting trimed strings 
    text("Trimmed string is: " + A, 50, 30); 
    text("Trimmed strings are: " + B, 50, 60); 
    text("Trimmed strings are: " + C, 50, 90); 
}

输出:

示例2:本示例使用trim()函数从字符串的开头和结尾删除制表符。

function setup() { 
  
    // Creating Canvas size 
    createCanvas(450, 120); 
} 
  
function draw() { 
  
    // Set the background color  
    background(220); 
  
    // Initializing the Strings and Array of strings 
    let String = "  GeeksforGeeks  "; 
    let Array1 = ["  5  ", "  6  ", "  7  "]; 
    let Array2 = ["  Delhi  ", 
                  "  Mumbai  ", 
                  "  Kolkata  ", 
                  "  Patna  "]; 
  
    // Calling to trim() function. 
    let A = trim(String); 
    let B = trim(Array1); 
    let C = trim(Array2); 
  
    // Set the size of text  
    textSize(16); 
  
    // Set the text color  
    fill(color('red')); 
  
    // Getting trimed strings 
    text("Trimmed string is: " + A, 50, 30); 
    text("Trimmed strings are: " + B, 50, 60); 
    text("Trimmed strings are: " + C, 50, 90); 
  
}

输出:

参考: https://p5js.org/reference/#/p5/trim



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 p5.js | trim() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。