I've had to split()
a large string into an array, whatever way it worked I'm now left with a space before each element.
我不得不将一个大字符串拆分成一个数组,无论它工作的方式如何,我现在在每个元素之前留下一个空格。
For example:
var array = [" hello"," goodbye"," no"];
How can I get rid of this?
我怎么能摆脱这个?
Split code as requested:
按要求拆分代码:
var depots = v.innerHTML.split(',');
4 个解决方案
#1
#2
3
Don't use Array#map()
to change each element in the array, when you can remove the spaces from the string itself when splitting it by a delimiter.
不要使用Array#map()来更改数组中的每个元素,只要在用分隔符拆分时就可以从字符串本身中删除空格。
Use String#trim()
with String#split()
with RegEx
将String#trim()与String#split()一起使用RegEx
str.trim().split(/\s*,\s*/)
The regex \s*,\s*
will match comma surrounded by any number(including zero) of spaces.
正则表达式\ s *,\ s *将匹配由任意数字(包括零)空格包围的逗号。
Live Demo:
var regex = /\s*,\s*/;
document.getElementById('textbox').addEventListener('keyup', function() {
document.getElementById('result').innerHTML = JSON.stringify(this.value.trim().split(regex));
});
<input type="text" id="textbox" />
<pre id="result"></pre>
trim()
will remove the leading and trailing spaces from the string and split
with the RegEx ,\s*
will split the string by comma followed by any number of spaces.
trim()将从字符串中删除前导和尾随空格并使用RegEx拆分,\ s *将用逗号分隔字符串,后跟任意数量的空格。
The same results can also be achieved using the negated RegEx with String#match
with global flag.
使用带有字符串#匹配全局标志的否定RegEx也可以实现相同的结果。
str.match(/[^,\s]+/g)
Why
map()
should not be used?为什么不应该使用map()?
Using map
in this case requires extra overheads. First, split
the string by ,
and then iterate over each of the element of the array and remove the leading and trailing spaces using trim
and then updating the array. This is bit slower than using the split
with above RegEx.
在这种情况下使用map需要额外的开销。首先,将字符串拆分,然后迭代遍历数组的每个元素,并使用trim删除前导和尾随空格,然后更新数组。这比使用上面的RegEx拆分慢一点。
#3
0
Loop through each array element and trim it . As like below
遍历每个数组元素并修剪它。如下所示
for (int i = 0; i < array.length; i++)
trimmedArray[i] = array[i].trim();
#4
-3
You need these two jQuery functions:
你需要这两个jQuery函数:
1.) iterate through array element with ability to edit items: http://api.jquery.com/jquery.map/
1.)遍历数组元素,具有编辑项目的能力:http://api.jquery.com/jquery.map/
2.) remove blank spaces from beginning and end of a string: http://api.jquery.com/jQuery.trim/
2.)从字符串的开头和结尾删除空格:http://api.jquery.com/jQuery.trim/
Use them this way:
以这种方式使用它们:
array = $.map(array, function(value) { return value.trim();});
Check this JSFiddle: https://jsfiddle.net/L00eyL4x/49/
检查这个JSFiddle:https://jsfiddle.net/L00eyL4x/49/
#1
8
您可以使用.map和.trim
var array = [" hello"," goodbye"," no"];
array = array.map(function (el) {
return el.trim();
});
console.log(array);
if you use ES2015
, you can do it shorter, with arrow function
如果您使用ES2015,您可以使用箭头功能缩短它
array = array.map(el => el.trim());
#2
3
Don't use Array#map()
to change each element in the array, when you can remove the spaces from the string itself when splitting it by a delimiter.
不要使用Array#map()来更改数组中的每个元素,只要在用分隔符拆分时就可以从字符串本身中删除空格。
Use String#trim()
with String#split()
with RegEx
将String#trim()与String#split()一起使用RegEx
str.trim().split(/\s*,\s*/)
The regex \s*,\s*
will match comma surrounded by any number(including zero) of spaces.
正则表达式\ s *,\ s *将匹配由任意数字(包括零)空格包围的逗号。
Live Demo:
var regex = /\s*,\s*/;
document.getElementById('textbox').addEventListener('keyup', function() {
document.getElementById('result').innerHTML = JSON.stringify(this.value.trim().split(regex));
});
<input type="text" id="textbox" />
<pre id="result"></pre>
trim()
will remove the leading and trailing spaces from the string and split
with the RegEx ,\s*
will split the string by comma followed by any number of spaces.
trim()将从字符串中删除前导和尾随空格并使用RegEx拆分,\ s *将用逗号分隔字符串,后跟任意数量的空格。
The same results can also be achieved using the negated RegEx with String#match
with global flag.
使用带有字符串#匹配全局标志的否定RegEx也可以实现相同的结果。
str.match(/[^,\s]+/g)
Why
map()
should not be used?为什么不应该使用map()?
Using map
in this case requires extra overheads. First, split
the string by ,
and then iterate over each of the element of the array and remove the leading and trailing spaces using trim
and then updating the array. This is bit slower than using the split
with above RegEx.
在这种情况下使用map需要额外的开销。首先,将字符串拆分,然后迭代遍历数组的每个元素,并使用trim删除前导和尾随空格,然后更新数组。这比使用上面的RegEx拆分慢一点。
#3
0
Loop through each array element and trim it . As like below
遍历每个数组元素并修剪它。如下所示
for (int i = 0; i < array.length; i++)
trimmedArray[i] = array[i].trim();
#4
-3
You need these two jQuery functions:
你需要这两个jQuery函数:
1.) iterate through array element with ability to edit items: http://api.jquery.com/jquery.map/
1.)遍历数组元素,具有编辑项目的能力:http://api.jquery.com/jquery.map/
2.) remove blank spaces from beginning and end of a string: http://api.jquery.com/jQuery.trim/
2.)从字符串的开头和结尾删除空格:http://api.jquery.com/jQuery.trim/
Use them this way:
以这种方式使用它们:
array = $.map(array, function(value) { return value.trim();});
Check this JSFiddle: https://jsfiddle.net/L00eyL4x/49/
检查这个JSFiddle:https://jsfiddle.net/L00eyL4x/49/