In this JavaScript code if the variable data
does not have that character .
then what will split return?
x = data.split('.');
Will it be an array of the original string?
In the case of splitting an empty string, the first mode (no argument) will return an empty list because the whitespace is eaten and there are no values to put in the result list. In contrast, the second mode (with an argument such as \n ) will produce the first empty field.
The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings.
The string manipulation function in Python used to break down a bigger string into several smaller strings is called the split() function in Python. The split() function returns the strings as a list.
The Javadoc for split(String regex) does not indicate that null cannot be returned. @Paul: No, but if null could be returned, it (or the Pattern#split docs) would say so.
Yes, as per ECMA262 15.5.4.14 String.prototype.split (separator, limit)
, if the separator is not in the string, it returns a one-element array with the original string in it. The outcome can be inferred from:
Returns an Array object into which substrings of the result of converting this object to a String have been stored. The substrings are determined by searching from left to right for occurrences of separator; these occurrences are not part of any substring in the returned array, but serve to divide up the String value.
If you're not happy inferring that, you can follow the rather voluminous steps at the bottom and you'll see that's what it does.
Testing it, if you type in the code:
alert('paxdiablo'.split('.')[0]);
you'll see that it outputs paxdiablo
, the first (and only) array element. Running:
alert('pax.diablo'.split('.')[0]); alert('pax.diablo'.split('.')[1]);
on the other hand will give you two alerts, one for pax
and one for diablo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With