Reputation: 177
I am newbie to web development and JS in particular.
I am using a String object method split on this string:
let str = 'Look at me '; //last character is space
console.log(str.split(' '));
The separator for split is ' ' as well space. I expect to have an array of 3 items as well as I understan how split works. However, I got this:
['Look', 'at', 'me', '']
Can somebody explain please to me why does this happen? Thanks in advance!
Upvotes: 3
Views: 781
Reputation: 3719
split(separator, limit)
splits a String every time separator
occurs. In your case separator
is also the last char in the string and that means it split there although there is nothing afterwards (leading to an empty string ''
)
Use trim()
to remove leading and trailing whitespaces, so there won't be an empty string at the end of your split array.
let str = 'Look at me '; //last character is space
console.log(str.trim().split(" "));
Upvotes: 1
Reputation: 8056
ECMA-262 specifies how split()
is implemented in JavaScript.
It's quite hard to read, but here's my stab at interpreting it…
Step 14 specifies that the split()
always ends by grabbing the slice up to where the string ends:
- Let T be a String value equal to the substring of S consisting of the characters at positions p (inclusive) through s (exclusive).
That slice is then added to the array of outputs:
- Call the [[DefineOwnProperty]] internal method of A with arguments ToString(lengthA), Property Descriptor {[[Value]]: T, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false.
Remember that split()
's argument is a delimiter (also known as a separator).
"ABC".split("B")
treats "B"
as a delimiter, delimiting the substrings "A"
and "C"
.
"ABC".split("C")
treats "C"
as a delimiter, delimiting the substrings "AB"
and ""
. Yes, empty string is still a string, and thus "C"
separates the substring "AB"
from the substring ""
.
Your example is the same, except instead of "C"
, you have a space character, " "
. Space is not special in any way, so the same rule applies: it separates the preceding substring from the substring after it (which happens to be empty).
Upvotes: 4
Reputation: 69
This is happening because str.split(" ")
returns string values until input has no more characters and changes to the next array element every time the NEXT character is the input character you passed. And because your input has a space character in its end the last returned value from split method will be empty.
Upvotes: 1
Reputation: 1335
The split()
function effectively divides a string into pieces and puts those pieces into an array. The first parameter to the function (' ')
is an empty space, meaning that the division happens wherever there is a blank space. Your string, 'Look at me '
has three spaces in it, or three divisions. If you were to take a long piece of cake and cut it three times, you would end up with four pieces. In the real world, you of course wouldn't have a fourth piece that is effectively "empty" or blank, but in computer science, you just get an extra piece that doesn't have anything in it.
The solution, as noted by @ppasler is to trim any excess whitespace from the string you're attempting to split. trim()
removes whitespace from the beginning and end of a string, meaning that trim('Look at me ')
would result in 'Look at me'
. If you then use split on that resulting string, you will get: ['Look', 'at', 'me']
.
Upvotes: 3
Reputation: 6698
It happens because your string contains a space at the end. That last space of your string is "split", with a word on the left ('me'), and an empty string on the right (''); That's just the way split
works. You might check this for reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split.
If you would like to remove the last element, just remove that extra space at the end. You can do that using trim
:
let str = 'Look at me '; //last character is space
console.log(str.trim().split(' '));
Upvotes: 1