自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 前端
在今天的文章中,我們將學習如何輕松地在 JavaScript 中的字符串字符之間包含空格。

在今天的文章中,我們將學習如何輕松地在 JavaScript 中的字符串字符之間包含空格。

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

要在字符串的字符之間添加空格,請對字符串調用 split() 方法以獲取字符數(shù)組,然后對該數(shù)組調用 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() 方法使用指定的分隔符將字符串拆分為子字符串數(shù)組。

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

通過使用空字符串 ('') 作為分隔符,我們將所有字符串字符拆分為單獨的數(shù)組元素。

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() 方法將數(shù)組中的每個字符串與分隔符組合在一起。 它返回一個包含串聯(lián)數(shù)組元素的新字符串。

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() 會使它成為數(shù)組中的一個單獨元素,該元素將與其他空格組合。

// 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() 方法返回一個新數(shù)組,其中僅包含原始數(shù)組中的元素,從傳遞給 filter() 的測試回調函數(shù)返回真值。 在空格 (' ') 上調用 trim() 會產生一個空字符串 (''),這在 JavaScript 中不是真值。 因此,從 filter() 返回的結果數(shù)組中排除了空格。

小技巧:

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

2. for…of 循環(huán)

對于命令的方法,我們可以使用 JavaScript for...of 循環(huán)在字符串的字符之間添加一個空格。

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前端開發(fā)
相關推薦

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表達式

2020-10-16 18:35:53

JavaScript字符串正則表達式

2023-10-20 15:58:27

Python刪除指定字符

2021-04-15 00:16:18

JavaString字符串

2021-03-26 08:36:35

JavaScript字符串開發(fā)

2019-12-25 15:41:50

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

2011-07-11 15:36:44

JavaScript

2020-05-12 08:53:15

JavaScript字符串處理庫

2020-12-31 07:56:02

JavaScript 字符串技巧

2010-09-09 11:48:00

SQL函數(shù)字符串

2021-01-09 23:11:33

SQL數(shù)據(jù)庫字母

2020-08-01 16:19:13

JavaScript字符串開發(fā)
點贊
收藏

51CTO技術棧公眾號