เพื่อปรับเปลี่ยนสตริง คุณสามารถใช้ toLowerCase() และ toUpperCase() สมมติสตริงของเราคือ
var sentence = "tHIS iS tHE JavaScript pROGRAM";
เพื่อปรับเปลี่ยนและแสดงในรูปแบบที่ถูกต้อง โค้ดมีดังนี้
ตัวอย่าง
var sentence = "tHIS iS tHE JavaScript pROGRAM"; function modifyStringWithMultipleMethods(sentence) { return sentence.charAt(0).toUpperCase() + sentence.slice(1).toLowerCase(); } console.log(modifyStringWithMultipleMethods(sentence));
เพื่อรันโปรแกรมด้านบน คุณต้องใช้คำสั่งต่อไปนี้
node fileName.js.
ที่นี่ชื่อไฟล์ของผมคือ demo51.js
ผลลัพธ์
จะได้ผลลัพธ์ดังนี้
PS C:\Users\Amit\JavaScript-code> node demo51.js This is the JavaScript program