當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js splitTokens()用法及代碼示例

p5.j​​s中的splitTokens()函數用於使用定界符將輸入字符串分成數據段。該分隔符可以是在輸入字符串的每一段之間使用的任何字符串或符號。

用法:

split( string, delimiter )

參數:該函數接受上述和以下描述的兩個參數:


  • String:這是要分割的輸入字符串。
  • Delimiter:這是用於分隔輸入字符串數據的任何字符串或符號。

返回值:它返回以逗號(,)分隔的輸入字符串的分割數據。

以下程序說明了p5.js中的splitTokens()函數:

示例1:本示例使用splitTokens()函數使用定界符將輸入字符串分成數據段。

function setup() {  
   
    // Create Canvas 
    createCanvas(500, 60);  
}  
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing the Strings 
    let String = 'GeeksforGeeks/Geeks/Geek/gfg';   
      
    // Calling to splitTokens() function 
    let A = splitTokens(String, '/'); 
      
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting splitted string 
    text("Splitted string is: " + A, 50, 30);   
} 

輸出:

示例2:本示例使用splitTokens()函數使用定界符將輸入字符串分成數據段。

function setup() {  
   
    // Create Canvas 
    createCanvas(450, 60);  
}  
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing the Strings 
    let String = '0&11&222&3333';   
      
    // Calling to splitTokens() function. 
    let A = splitTokens(String, '&'); 
      
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting splitted string 
    text("Splitted string is: " + A, 50, 30);   
} 

輸出:

注意:splitTokens()和split()之間的區別在於,splitTokens()函數使用一定範圍的字符進行拆分,而split()函數則使用特定的字符或序列。

參考: https://p5js.org/reference/#/p5/splitTokens



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 p5.js | splitTokens() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。