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


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


p5.j​​s中的char()函數用於將給定的字符串或數字值轉換為其相應的單字符字符串表示形式。如果輸入參數是字符串值,則首先將其內部轉換為等效的整數,然後將其單字符字符串表示為輸出,當將字符串或數字數組作為參數時,它將輸出作為單個數組中每個元素的單字符字符串。

用法:

char( value )

參數:該函數接受單個參數值,該值將轉換為其單字符字符串表示形式。該值可以是數字,數字字符串和字符串或數字數組。


返回值:它返回轉換後的單字符字符串表示形式。

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

示例1:本示例使用char()函數將輸入值轉換為相應的字符。

function setup() {  
   
    // Creating Canvas size 
    createCanvas(600, 230);  
}  
   
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing some values 
    let Value1 = 66; 
    let Value2 = 67; 
    let Value3 = "65"; 
    let Value4 = 62;  
    let Value5 = "62"; 
    let Value6 = 90; 
     
    // Calling to char() function. 
    let A = char(Value1); 
    let B = char(Value2); 
    let C = char(Value3); 
    let D = char(Value4); 
    let E = char(Value5); 
    let F = char(Value6); 
       
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting character representation 
    text("Character representation of value 66 is: " + A, 50, 30); 
    text("Character representation of value 67 is: " + B, 50, 60); 
    text("Character representation of string '65' is: " + C, 50, 90); 
    text("Character representation of value 62 is: " + D, 50, 110); 
    text("Character representation of string '62' is: " + E, 50, 140); 
    text("Character representation of value 90 is: " + F, 50, 170); 
} 

輸出:

示例2:本示例使用char()函數將輸入值轉換為相應的字符。

function setup() {  
   
    // Creating Canvas size 
    createCanvas(600, 140);  
}  
   
function draw() {  
       
    // Set the background color  
    background(220);  
     
    // Initializing some values 
    let Value1 = [66, 67]; 
    let Value2 = ["66", "67"]; 
    let Value3 = ["66", "67", "90"] 
     
    // Calling to char() function. 
    let A = char(Value1); 
    let B = char(Value2); 
    let C = char(Value3); 
       
    // Set the size of text  
    textSize(16);  
       
    // Set the text color  
    fill(color('red'));  
     
    // Getting character representation 
    text("Character representation of array [66, 67] is: " + A, 50, 30); 
    text("Character representation of array ['66', '67'] is: " + B, 50, 60); 
    text("Character representation of array ['66', '67', '90'] is: " + C, 50, 90); 
}     

輸出:

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



相關用法


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