3

With this code:

"\t\ttest\t\t\t".split(/\t/)

I expect the following result:

 => ["", "", "test", "", "", ""]

But the result is:

=> ["", "", "test"]

Why?

1 Answer 1

7

If the limit parameter is omitted, trailing null fields are left off the returned array. If it is negative, they are returned:

# Supply -1 as the limit parameter
"\t\ttest\t\t\t".split(/\t/, -1)
=> ["", "", "test", "", "", ""]

This is detailed in the String.split() documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool! thank you! I found that the official document does mention the limit, but I don't know the "null" means the empty string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.