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


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


p5.j​​s中的join()函数用于使用分隔符将字符串输入数组连接成单个字符串。该分隔符可以是任何在输入数组的两个字符串之间定位的字符。

用法:

join(Array, Separator)

参数:该函数接受上述和以下描述的两个参数:


  • 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



相关用法


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