Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

แปลงสตริงเป็น Camel Case ใน JavaScript

Camel case คือการเขียนคำให้แต่ละคำขึ้นต้นด้วยตัวใหญ่ ไม่มี space หรือ punctuation เช่น Concurrent hash maps → ConcurrentHashMaps

วิธี implement ฟังก์ชันแปลงสตริงเป็น camel case

ตัวอย่าง

function camelize(str) {    // Split the string at all space characters    return str.split(' ')       // get rid of any extra spaces using trim       .map(a => a.trim())       // Convert first char to upper case for each word       .map(a => a[0].toUpperCase() + a.substring(1))       // Join all the strings back together       .join("") } console.log(camelize("Concurrent hash maps"))

ผลลัพธ์

ConcurrentHashMaps