Zero-Length String Being Returned from String#Split

Zero-length string being returned from String#split

"abcabc".split("b") #=> ["a", "ca", "c"]
"abcabc".split("a") #=> ["", "bc", "bc"]
"abcabc".split("c") #=> ["ab", "ab"]

Suppose you were splitting on a comma. What behaviour would you expect from
",bc,bc".split(',')? It's not different with splitting on 'a'. For the third example, split omits the trailing empties by default.

Why does ||.split(\\|).length return 0 and not 3?

According to the javadoc:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The javadoc for split(String, int) elaborates:

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

(emphasis mine)

So to return an array of empty strings, call "||".split("\\|", -1)

strings.Split never returns nil or zero length slice?

You are right: strings.Spit() will never return a nil value. The result will be of type []string with at least one element containing the given string.

Whether you can remove the if block depends: Does your code have a problem if len(ips) < 2? If it does not you can safely remove the if block.

If however e.g. you are only interested in ips[1] then you definitely need to check first.

Java String split removed empty values

split(delimiter) by default removes trailing empty strings from result array. To turn this mechanism off we need to use overloaded version of split(delimiter, limit) with limit set to negative value like

String[] split = data.split("\\|", -1);

Little more details:

split(regex) internally returns result of split(regex, 0) and in documentation of this method you can find (emphasis mine)

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Exception:

It is worth mentioning that removing trailing empty string makes sense only if such empty strings were created by the split mechanism. So for "".split(anything) since we can't split "" farther we will get as result [""] array.

It happens because split didn't happen here, so "" despite being empty and trailing represents original string, not empty string which was created by splitting process.

why is my string.length 0 if I split this string?

You need s.split("\\.") because the argument to split is a regular expression. The . character in a regular expression means "any character", and you need to escape it with the backslash to have it mean "dot".

why is string.split returning extra empty entries in this example?

This should work.

function formatDate(userDate) {

// format from M/D/YYYY to YYYYMMDD

console.log(userDate);

var dateParts = userDate.split("/");

return dateParts[2] + dateParts[0] + dateParts[1];

}

console.log(formatDate("12/31/2014"));

When does String.split return an empty array?

split(regex) returns result of split(regex,0) where 0 is limit. Now according to documentation (limit is represented by n)

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

(emphasis mine)

It means that in case of code like

"ababaa".split("a")

at first you will get array ["", "b","b","",""] but then trailing empty string swill be removed, so you will end up with array ["","b","b"]

BUT if your string contains text which can be matched by split pattern entirely, like

"ababab".split("ab")

at first resulting array will contain ["","","",""] (three splits), but then empty trailing strings will be removed. Since all of them are trailing empty strings all of them will be removed from array and empty array will be returned [] (array with size 0).

So to get empty array as result you need to split on string which is build from parts which can be matched by split pattern. This means that in case of yourString.split(" ") the yourString must contain only spaces (at least one, see BTW for more info)


BTW if original string would be empty "" and we call "".split("any-value") then splitting won't happen (split nothing makes no sense). In such case array containing original string [""] will be returned and that empty string will NOT be removed because it was not result of splitting.

In other words removing trailing empty strings makes sense only if these empty strings ware created as result of splitting, like "abaa".split("a") initially creates ["","b", "",""]. So when splitting didn't happen "cleanup" is not required. In such case result array will contain original string as explained in my earlier answer on this subject.

JavaScript split gives array size one instead of zero for empty string

Everything before the first match is returned as the first element. Even if the String is Empty. It's not null

If you want split and return an 0 length Array, I recommand you to use the underscore.string module and the words method :

_str.words("", ",");
// => []

_str.words("Foo", ",");
// => [ 'Foo' ]

Why are empty strings returned in split() results?

str.split complements str.join, so

"/".join(['', 'segment', 'segment', ''])

gets you back the original string.

If the empty strings were not there, the first and last '/' would be missing after the join().



Related Topics



Leave a reply



Submit