p5.js |修剪()函数
p5.js 中的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