p5.js中的split()函數用於使用定界符將輸入字符串分成多個部分。該分隔符可以是在輸入字符串的每一段之間使用的任何字符串或符號。
用法:
split(String, Delimiter)
參數:該函數接受以下兩個參數:
- String:這是要分割的輸入字符串。
- Delimiter:這是用於分隔輸入字符串數據的任何字符串或符號。
返回值:它返回輸入字符串的分割數據。
以下程序說明了p5.js中的split()函數。
示例1:本示例使用split()函數使用Delemeter將輸入字符串分成多個子字符串。
function setup() {  
   
    // Creating Canvas size 
    createCanvas(450, 150);  
}  
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing the Strings 
    let String = 'GeeksforGeeks/Geeks/Geek/gfg';   
      
    // Calling to split() function. 
    let A = split(String, '/'); 
      
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting splitted string 
    text("Splitted string is: " + A[0], 50, 30);   
    text("Splitted string is: " + A[1], 50, 60);   
    text("Splitted string is: " + A[2], 50, 90);   
    text("Splitted string is: " + A[3], 50, 120); 
} 輸出:

示例2:本示例使用split()函數使用Delemeter將輸入字符串分成多個子字符串。
function setup() {  
   
    // Creating Canvas size 
    createCanvas(450, 150);  
}  
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing the Strings 
    let String = '0&11&222&3333';   
      
    // Calling to split() function. 
    let A = split(String, '&'); 
      
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting splitted string 
    text("Splitted string is: " + A[0], 50, 30);   
    text("Splitted string is: " + A[1], 50, 60);   
    text("Splitted string is: " + A[2], 50, 90);   
    text("Splitted string is: " + A[3], 50, 120); 
} 輸出:

參考: https://p5js.org/reference/#/p5/split
相關用法
- Node.js split()用法及代碼示例
- Javascript String split()用法及代碼示例
- d3.js d3.set.add()用法及代碼示例
- PHP end()用法及代碼示例
- PHP dir()用法及代碼示例
- p5.js nfp()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | split() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
