使用 JavaScript 用多種方法修改字串的更好方法


要修改字串,你可以使用 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

更新時間: 03-Sep-2020

353 次瀏覽

開啟你的 職業

完成本課程並獲得認證

開始
廣告
© . All rights reserved.