除去 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