如何在 JavaScript 中的字符串的字符之间添加空格

开发 前端
在今天的文章中,我们将学习如何轻松地在 JavaScript 中的字符串字符之间包含空格。

在今天的文章中,我们将学习如何轻松地在 JavaScript 中的字符串字符之间包含空格。

1. String split() 和 Split join() 方法

要在字符串的字符之间添加空格,请对字符串调用 split() 方法以获取字符数组,然后对该数组调用 join() 方法以使用空格分隔符连接字符。

例如:

function addSpace(str) {
return str.split('').join(' ');
}
const str1 = 'coffee';
const str2 = 'banana';
console.log(addSpace(str1)); // c o f f e e
console.log(addSpace(str2)); // b a n a n a

String split() 方法使用指定的分隔符将字符串拆分为子字符串数组。

const str1 = 'coffee,milk,tea';
const str2 = 'sun-moon-star';
console.log(str1.split(',')); // [ 'coffee', 'milk', 'tea' ]
console.log(str2.split('-')); // [ 'sun', 'moon', 'star' ]

通过使用空字符串 ('') 作为分隔符,我们将所有字符串字符拆分为单独的数组元素。

const str1 = 'coffee';
const str2 = 'banana';
// Passing an empty string ('') to the split method
// [ 'c', 'o', 'f', 'f', 'e', 'e' ]
console.log(str1.split(''));
// [ 'b', 'a', 'n', 'a', 'n', 'a' ]
console.log(str2.split(''));

String join() 方法将数组中的每个字符串与分隔符组合在一起。 它返回一个包含串联数组元素的新字符串。

const arr = ['a', 'b', 'c', 'd'];
console.log(arr.join(' ')); // a b c d
console.log(arr.join('-')); // a-b-c-d
console.log(arr.join('/')); // a/b/c/d

因此,将空格字符传递给 join() 会在生成的连接中用空格分隔字符。

在某些情况下,字符串已经在某些字符之间包含空格。 在这种情况下,我们的方法会在字符之间添加更多的空格。

function addSpace(str) {
return str.split('').join(' ');
}
// These strings have spaces between some characters
const str1 = 'co ffee';
const str2 = 'bana na';
console.log(addSpace(str1)); // c o f f e e
console.log(addSpace(str2)); // b a n a n a

这是因为空格 (' ') 也是一个字符,就像一个字母,调用 split() 会使它成为数组中的一个单独元素,该元素将与其他空格组合。

// These strings have spaces between some characters
const str1 = 'co ffee';
const str2 = 'bana na';
// The space characters are separate elements of the
// array from split()
/**
* [
'c', 'o', ' ',
' ', 'f', 'f',
'e', 'e'
]
*/
console.log(str1.split(''));
/**
* [
'b', 'a', 'n',
'a', ' ', ' ',
'n', 'a'
]
*/
console.log(str2.split(''));

如果我们想避免字符的多重间距,我们可以在 split() 和 join() 之间插入对 filter() 方法的调用。

function addSpace(str) {
return str
.split('')
.filter((item) => item.trim())
.join(' ');
}
// The strings have spaces between some characters
const str1 = 'co ffee';
const str2 = 'bana na';
console.log(addSpace(str1)); // c o f f e e
console.log(addSpace(str2)); // b a n a n a

Array filter() 方法返回一个新数组,其中仅包含原始数组中的元素,从传递给 filter() 的测试回调函数返回真值。 在空格 (' ') 上调用 trim() 会产生一个空字符串 (''),这在 JavaScript 中不是真值。 因此,从 filter() 返回的结果数组中排除了空格。

小技巧:

在 JavaScript 中,只有六个假值:false、null、undefined、0、' '(空字符串)和 NaN。 其他所有值都是真实的。

2. for…of 循环

对于命令的方法,我们可以使用 JavaScript for...of 循环在字符串的字符之间添加一个空格。

function addSpace(str) {
// Create a variable to store the eventual result
let result = '';
for (const char of str) {
// On each iteration, add the character and a space
// to the variable
result += char + ' ';
}
// Remove the space from the last character
return result.trimEnd();
}
const str1 = 'coffee';
const str2 = 'banana';
console.log(addSpace(str1)); // c o f f e e
console.log(addSpace(str2)); // b a n a n a

要处理前面讨论的情况,其中字符串在某些字符之间有空格,请在每次迭代的字符上调用 trim(),并添加一个 if 检查以确保它是真实的,然后再将它和空格添加到累积结果中:


function addSpace(str) {
// Create a variable to store the eventual result
let result = '';
for (const char of str) {
// On each iteration, add the character and a space
// to the variable
// If the character is a space, trim it to an empty
// string, then only add it if it is truthy
if (char.trim()) {
result += char + ' ';
}
}
// Remove the space from the last character
return result.trimEnd();
}
const str1 = 'co ffee';
const str2 = 'bana na';
console.log(addSpace(str1)); // c o f f e e
console.log(addSpace(str2)); // b a n a n a

总结

以上就是我今天跟你分享的关于JavaScript的基础知识,希望这些知识能够对你有用,如果你觉得有帮助的话,请记得点赞我,关注我,并将这篇文章分享给你的朋友。

责任编辑:华轩 来源: web前端开发
相关推荐

2011-07-11 16:00:22

字符串拼接

2020-09-03 10:13:49

JavaScript字符串pad

2017-12-11 13:50:17

LinuxBash子字符串

2015-06-09 14:43:36

javascript操作字符串

2021-03-11 18:44:39

字符串SQL表达式

2023-10-20 15:58:27

Python删除指定字符

2021-04-15 00:16:18

JavaString字符串

2020-10-16 18:35:53

JavaScript字符串正则表达式

2019-12-25 15:41:50

JavaScript程序员编程语言

2021-03-26 08:36:35

JavaScript字符串开发

2022-12-06 08:27:50

Bash脚本字符串

2010-10-11 15:57:35

MySQL清除字符串

2010-06-28 15:18:51

SQL Server

2021-08-26 09:46:22

JavaScript字符串URL

2021-09-13 10:20:49

Python数据程序

2021-01-09 23:11:33

SQL数据库字母

2009-11-24 19:33:07

PHP字符串中加入变量

2011-07-11 15:36:44

JavaScript

2020-05-12 08:53:15

JavaScript字符串处理库

2020-12-31 07:56:02

JavaScript 字符串技巧
点赞
收藏

51CTO技术栈公众号