Reputation: 1430
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(',');
Upvotes: 7
Views: 28381
Reputation: 495
Just one more thing to add in @Tushar's answer. Answer is great and well explained. One thing just missed that I think it's not question's requirement.
If user enter double comma instead of single in result an empty string will be added/return into the array. I just did a little change to the code.
.filter(elem => elem.length !== 0)
filter()
iterate into each element of the array and check if it is empty or not by String's .length
property. If an element length will not be equal to zero it will be added/return as element into array. And at the last a brand new filtered array will be returned.
var regex = /\s*,\s*/;
document.getElementById('textbox').addEventListener('keyup', function() {
document.getElementById('result').innerHTML = JSON.stringify(this.value.trim().split(regex).filter(elem => elem.length !== 0));
});
<input type="text" id="textbox" />
<pre id="result"></pre>
Upvotes: 0
Reputation: 1272
var array = [" hello"," goodbye"," no"].toString().replace(/\s*\,\s*/g, ",").trim().split(",");
console.log(array)
Upvotes: 1
Reputation: 77482
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
array = array.map(el => el.trim());
Upvotes: 24
Reputation: 72
You need these two jQuery functions:
1.) iterate through array element with ability to edit items: http://api.jquery.com/jquery.map/
2.) remove blank spaces from beginning and end of a string: 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/
Upvotes: -5
Reputation: 87203
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.
Use String#trim()
with String#split()
with RegEx
str.trim().split(/\s*,\s*/)
The regex \s*,\s*
will match comma surrounded by any number(including zero) of spaces.
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.
The same results can also be achieved using the negated RegEx with String#match
with global flag.
str.match(/[^,\s]+/g)
Why
map()
should not be used?
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.
Upvotes: 11
Reputation: 923
Loop through each array element and trim it . As like below
for (int i = 0; i < array.length; i++)
trimmedArray[i] = array[i].trim();
Upvotes: -1