如何從 JavaScript 中的字串中刪除“,”


我們得到了一個主字串和一個子字串,我們的任務是建立一個函式 shedString(),它接受這兩個引數並返回一個沒有子字串的主字串版本。

例如 −

shedString('12/23/2020', '/');

應該返回一個字串 −

'12232020'

現在讓我們為該函式編寫程式碼 −

範例

const shedString = (string, separator) => {
   //we split the string and make it free of separator
   const separatedArray = string.split(separator);
   //we join the separatedArray with empty string
   const separatedString = separatedArray.join("");
   return separatedString;
}
const str = shedString('12/23/2020', '/');
console.log(str);

輸出

此程式碼在控制檯中的輸出為 −

12232020

更新於: 19-Aug-2020

135 次瀏覽

開啟你的 職業道路

完成課程認證

開始你的學習
廣告
© . All rights reserved.