p5.js |加入()函数
p5.js 中的join()函数用于使用分隔符将输入的字符串数组连接成单个字符串。这个分隔符可能是任何字符,它们的位置位于输入数组的两个字符串之间。
句法:
join(Array, Separator)
参数:该函数接受上面提到的两个参数,如下所述:
- 数组:这是要连接的字符串数组。
- 分隔符:这是要放置在输入数组的两个字符串之间的任何字符。
返回值:它返回连接的字符串。
下面的程序说明了 p5.js 中的 join()函数:
示例 1:此示例使用 join()函数连接输入数组元素。
function setup() {
// Creating Canvas size
createCanvas(450, 120);
}
function draw() {
// Set the background color
background(220);
// Initializing the Array of strings
let Array1 = ["Geeks", "for", "Geeks"];
let Array2 = ["1", "2", "3"];
let Array3 = ["a", "A", "b", "B"];
// Initializing the separator character
let Separator1 = "/";
let Separator2 = "&";
let Separator3 = "*";
// Calling to join() function.
let A = join(Array1, Separator1);
let B = join(Array2, Separator2);
let C = join(Array3, Separator3);
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting joined strings
text("Joined string is: " + A, 50, 30);
text("Joined string is: " + B, 50, 60);
text("Joined string is: " + C, 50, 90);
}
输出:
示例 2:此示例使用 join()函数连接输入数组元素。
function setup() {
// Creating Canvas size
createCanvas(450, 120);
}
function draw() {
// Set the background color
background(220);
// Calling join() function over the parameter
// Array of strings and separator
let A = join(["kolkata", "Mumbai", "Delhi"], " ");
let B = join(["Geeks", "for", "Geeks"], "");
let C = join(["Ram", "Shyam", "Geeks", "Rahim"], "+");
// Set the size of text
textSize(16);
// Set the text color
fill(color('red'));
// Getting joined strings
text("Joined string is: " + A, 50, 30);
text("Joined string is: " + B, 50, 60);
text("Joined string is: " + C, 50, 90);
}
输出:
参考: https://p5js.org/reference/#/p5/join