简体   繁体   English

向 javascript 中的数组添加空格

[英]Add whitespace to an array in javascript

Simply put, I want to split a string in javascript into an array, which should also include all the whitespaces in the original string.简单来说,就是想把 javascript 中的一个字符串拆分成一个数组,这个数组还应该包含原字符串中的所有空格。

Ex: "Hi There" should be broken down to ['H','i',' ','T','h','e','r','e'] I tried using str.split('') only to find the results as ['H','i','','T','h','e','r','e'] I want a whitespace after "i" in "Hi" here in this example例如: "Hi There"应该分解为['H','i',' ','T','h','e','r','e']我尝试使用str.split('')只找到结果为['H','i','','T','h','e','r','e']我想要在"i"之后的空格"Hi"在这个例子中

Try this:尝试这个:

 const result = "Hi There".split(""), solution = [..."Hi There"]; console.log(result); console.log(solution);

I tried the same as yours, and it worked, ie "Hi There".split('') gives a space where it finds one:我尝试了与您相同的方法,并且成功了,即"Hi There".split('')给出了一个空间,它可以找到一个空间:

 console.log("Hi There".split(''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM