除去 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP