除去 JavaScript 中字串中的空格


我們需要編寫一個 JavaScript 函式,該函式接收一個字串並返回一個新字串,新字串中原始字串中的所有字元的空格已被移除。

示例

此程式碼為:

const str = "This is an example string from which all whitespaces will be
removed";
const removeWhitespaces = str => {
   let newStr = '';
   for(let i = 0; i < str.length; i++){
      if(str[i] !== " "){
         newStr += str[i];
      }else{
         newStr += '';
      };
   };
   return newStr;
};
console.log(removeWhitespaces(str));

輸出

控制檯中的輸出:

Thisisanexamplestringfromwhichallwhitespaceswillberemoved

更新於:15-Oct-2020

93 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.